Some findings regarding records, sealed types and switch expressions

Brian Goetz brian.goetz at oracle.com
Thu Aug 22 22:01:29 UTC 2019



> 1) Questions (sealed)
>
> 1.1) Question: Do make generics in the permits section sense?
>
>    I can’t find a use case. The example below is misleading for a user.
>
>      // why restrict an implementation to specific generic types?
>      sealed interface A permits B<String> {}
>      class B<T> implements A
>
>      A a = new B<>(1);

The permits clause should be a list of _classes_, not of _types_. If the 
compiler accepts the above, that's a compiler bug.

> 1.2) Error: A generic sealed interface cannot be subclassed
>
>      sealed interface A<T> permits B<T> {}
>      class B<T> implements A<T> {}
>
>      sealed interface A<T> permits B {}
>      class B<T> implements A<T> {}
>
>      sealed interface A<T> permits B {}
>      class B implements A {}
>
>     In all three examples we get "cannot inherit from sealed class: A"

I would expect the latter two sets of examples to work (the last example 
merely implements the raw type A, rather than the parameterized type 
A<T>.)  This sounds like a bug.

> 1.3) Error: switch cases on generic records do not work

Which branch are you running against?  Deconstruction patterns in switch 
are not yet implemented, except experimentally in some branch that may 
or may not be parented on records-and-sealed.  In any case, the 
combination of these features is definitely bleeding edge at this 
point.  Our goal for the short term is to stabilized records and sealed 
types on their own, and add the pattern support after.

I'll let Jan answer on the details of what the patterns-deconstruction 
branch currently does.

> 2.4) Question: Are instanceof checks planned in switch expressions?
>
>      record A();
>
>      var res = switch(new Object()) {
>          case A -> 1 // instanceof A check
>          default -> 0
>      };

Yes, eventually, but its not clear whether the binding variable will be 
required:

     case A a -> 1

or whether omitting the binding variable will be OK.  I am worried about 
ambiguities in `identifier` as a pattern, since it could be a constant 
pattern or a type pattern.

> 2.5) Question: Best way to implement singletons?
THis is an open question ... there's some overlap between enum and 
records that we're still working through.



More information about the amber-dev mailing list