My experience with Sealed Types and Data-Oriented Programming

John Rose john.r.rose at oracle.com
Fri Sep 9 18:46:12 UTC 2022


On 9 Sep 2022, at 6:41, David Alayachew wrote:

> Hello Amber Team,
>
> I just wanted to share my experiences with Sealed Types and 
> Data-Oriented
> Programming.

Thank you for the well-written note, and for sharing your results.

>> I used reflection on my sealed interface. I got the sealed type class,
> called Class::getPermittedSubclasses, looped through the subclasses, 
> did an
> unsafe cast from Class<?> to Class<SealedInterface> (because
> ::getPermittedSubclasses doesn't do that on its own for some 
> reason???),

Good comment.

This feels like it *might* be a simple oversight.  If so we might choose 
to fix it.  Or it might well be a conscious decision to limit the impact 
of reflection on the language proper.  The sharpening of `String.class` 
to `Class<String>` actually requires complicated moves in the heart of 
the language, the JLS; such moves are very expensive.  Addressing this 
one would require JLS surgery.

Sometimes inconvenient stuff like this, like the no-go for generic enums 
JEP 301, is due to highly technical roadblocks which are too hard to 
remove.

>> I just wish that there was some similar centralized pathway between
> data-oriented programming and the outside world. Some way for me to 
> define
> on my sealed type, a method to give me a pathway to all of the 
> permitted
> subclasses. Obviously, I can build it on my own, but that is where 
> most of
> my pain points came from. Really, having some way to enforce that all 
> of my
> subclasses have a similar class level validation logic and a similar 
> class
> level factory/constructor method is what I am missing.

Good summary of a tricky design problem.  The images of “islands” is 
good.  We are also talking about a functor between categories, for those 
who find such ideas illuminating.

One thing I would try:  Attach grammar fragments or regexps-with-groups 
to each record using annotations.  Then build a tool which uses those 
annotations to weave a bridge between the islands.  It would require 
some sort of grammar generator.  I’d look for a combinator-based 
library, I think.  I don’t work in this area enough to know what’s 
current.  But I admire some Lua-based experiments [1] and think it’s 
likely some Java programmer has adapted those ideas to Java and lambdas 
and records etc.  If not maybe it’s time to do so.

Thanks!

— John

[1]: https://github.com/vsbenas/parser-gen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20220909/aebed712/attachment.htm>


More information about the amber-dev mailing list