Draft JEP: Sealed Classes

Remi Forax forax at univ-mlv.fr
Mon Mar 22 12:54:57 UTC 2021

> De: "Gavin Bierman" <gavin.bierman at oracle.com>
> À: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Lundi 22 Mars 2021 13:03:31
> Objet: Draft JEP: Sealed Classes

> Dear all,

> Now JDK 16 is out (yay!) it’s time for us to focus on finalizing Sealed Classes
> in JDK 17.

> I have written a draft JEP, which is unchanged (other than some superficial
> editorial changes) from JEP 397.

> [ https://openjdk.java.net/jeps/8260514 | https://openjdk.java.net/jeps/8260514
> ]

> (I think Brian’s proposal to consider extending the language in some way to
> support assignment and switch statements being total over sealed hierarchies is
> something that can be considered as a JEP in itself.)

> If you have any comments on the draft JEP, please let me know!

I think it's missing a discussion about lambdas, anonymous classes and local classes that can all extends/implements a sealed type. 
For Lambdas and anonymous classes, it's easy, they are anonymous, so have no name to list in the permits clause. 

For Local classes, they are not allowed because they may not be visible 
sealed interface I {} // I don't see A from here 
void foo() { 
record A() implements I {} 

But i think we may want to relax that rule a little to be able to declare a sealed type and an interface if they are at the same "nested level" 
void aTestMethod() { 
sealed interface I {} 
record A() implements I {} 

It's very convenient when you want different hierarchies when testing things like reflection inside JUnit. 

I also think we should add a discussion about why using the keyword "non-sealed" instead of something like "open", i.e why using a hyphen separated keyword instead of a "grammar local" keyword, 
because it's a question often asked when i've explained that JEP. Something along the line that hyphen separated keywords are a lexer issue so it's less work for all the Java ecosystem, all the tools that parse java code, than a using "grammar local" keyword which requires to tweak the parser or the grammar (or both). 

And some minor remarks, 
in the JEP, the example 
abstract sealed class Shape { 
class Circle extends Shape { ... } 
The class Circle should be declared static if we want to have the same behavior than the example above in the JEP. 

Also, getPermittedSubClasses() returns a java.lang.Class<?> not a java.lang.Class (<?> is missing). 

> Thanks,
> Gavin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20210322/787b4fc7/attachment.htm>

More information about the amber-spec-experts mailing list