Expand what permit can specify in Sealed Classes

Brian Goetz brian.goetz at oracle.com
Wed Nov 11 13:42:03 UTC 2020


We explored this option (e.g., "permits package") very early on in the 
feature design, and backed away from it.

Essentially, sealed types has a bimodal design center: extension 
control, and exhaustiveness.  The latter is about "I know that A=B+C, so 
a switch on A that covers B and C is exhaustive."  The compiler can use 
this information to provide better type checking.  While the first is 
more immediately obvious to Java developers (because we already have a 
system of access control), the second is actually more important in 
terms of building reliable code.  (If we had to pick only one of these, 
we'd pick the second.)

The extensions you are suggesting are all about extension control and 
not at all about exhaustiveness.  While that's not a fatal 
characteristic for these ideas, it is a red flag, because it's speaking 
exclusively to the minority motivation.

Basically, "permits module" or "permits package" is a purely syntactic 
convenience; you can express what you mean without it, and I don't think 
that the few cases where this list is long justifies the feature.  For 
the more complex examples ("permits supertypes of subtypes of classes 
with a Q in their name"), I think you'd be hard-pressed to come up with 
more than a handful of examples where it would be sensible.



On 11/11/2020 2:17 AM, Suminda Sirinath Salpitikorala Dharmasena wrote:
> Hello,
>
> Can the scope of the permit be expanded so following use cases are
> supported:
>
>     - package - permits package x.y.x, package a.b.c, S, T (S, T are classes
>     or interfaces)
>     - modules - permits module x.y.x, module a.b.c, S, T
>     - subtypes - permits extend A, extend B, S, T
>     - supertypes - permits super A, super B, S, T
>     - subtypes of supertype - permits extend super A, extend super B
>     (effectively not a subtype of A or B. Combining with extends can limit the
>     type hierarchy - e.g.: permits extends A, extends super B)
>     - class or interface - permits S, T (current implementation and
>     functionality)
>
> Suminda



More information about the amber-dev mailing list