[sealed] Sealed local classes?

John Rose john.r.rose at oracle.com
Fri Oct 11 21:12:02 UTC 2019


On Oct 9, 2019, at 2:26 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> 
> It is allowable, though somewhat silly, to put the `final` modifier on local classes.
> 
> From a hierarchy-protection point of view, allowing local classes to be `sealed` is also silly, as it cannot be extended from outside the method anyway, and even if it could, such types can't show up in APIs that are accessible from outside.
> 
> From an exhaustiveness point of view, though, one can imagine having a sealed local hierarchy (sum of records) that will be switched over within the method, though one would have to work pretty hard to imagine that.
> 
> Note that a local class cannot be a subtype of a sealed type declared outside the same method (*), since it can't be denoted in the permits clause.
> 
> (*) unless the permits clause is inferred.  Yuck.  Now a mangled name would go into the PermittedSubtypes attribute.
> 
> Proposal: ban `sealed` and `non-sealed` modifiers on _local_ classes and interfaces.

I’m OK with that ban as a starting position at least.  We can lift it later if we need to.
It’s a little like the ban on statics:  You can’t cut arbitrary class structure into an arbitrary block.




More information about the amber-spec-experts mailing list