New candidate JEP: 455: Primitive types in Patterns, instanceof, and switch (Preview)

Brian Goetz brian.goetz at oracle.com
Tue Sep 26 13:13:13 UTC 2023


Where null-explicit type patterns would help is when there are 
legitimate (human) ambiguities about whether nulls are wanted here.  In 
a world where we have T!/T? type patterns, you can think of the pattern 
`T t` as having _inferred nullity_; it is really either T! or T?, and we 
determine which from context.  Where we would want explicit nullity is 
either when the inference chooses something that you don't want, such as:

These mostly come down to things like:

     Box<Number> b = ...

     switch (b) {
         case Box(Integer i) -> ...
         case Box(Float f) -> ...
         case Box(Number! n) -> ...
     }

or where the inference feels "not obvious" and you want to be explicit.

As it turns out, your first example is nonsensical:


> ```
> switch (p) {
>     case Pair!(Pair?(A a, B b), Pair?(C c, D d)): ...
> }
> ```

Here, the nested pattern `Pair?(A a, B b)` makes no sense, because it is 
a record pattern, and a record pattern *cannot* match null (because it 
needs a non-null receiver on which to invoke the accessors and/or 
deconstructor.)  So even in a world with emotional type patterns, this 
pattern would be rejected by the compiler as illegal.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230926/2066933a/attachment.htm>


More information about the amber-dev mailing list