case null / null pattern (v2)

Brian Goetz brian.goetz at oracle.com
Tue Apr 19 20:02:22 UTC 2022


With the currently specified semantics, the second pattern is dead, 
because switches will only match null at the top level with a case 
null.  This was an accommodation to clarify that that the null-hostility 
of switch is a property of switch, not patterns, and make it more clear 
when switch will NPE.

Regardless, what you're asking for is a more precise remainder 
checking.  The first pattern matches all non-null Foo; because no case 
matches null, you're asking that we recognize that there is a dominance 
relationship here.  This is reasonable to consider (though harder, 
because null makes everything harder.)



On 4/18/2022 6:49 PM, Remi Forax wrote:
> I've found a way to encode the null pattern if you have a record
>
> record Foo(int x) { }
>
> Foo foo = ...
> return switch(foo) {
>    case Foo(int _) foo -> "i'm a foo not null here !";
>    case Foo fooButNull -> "i can be only null here !";
> };
>
> I wonder if allowing those two patterns, a record pattern and a type pattern using the same type is a good idea or not, it seems a great way to obfuscate thing.
>
> Rémi


More information about the amber-spec-observers mailing list