Pattern Matching for switch (Second Preview)

forax at univ-mlv.fr forax at univ-mlv.fr
Sat Sep 25 19:35:47 UTC 2021


----- Original Message -----
> From: "Brian Goetz" <brian.goetz at oracle.com>
> To: "Remi Forax" <forax at univ-mlv.fr>, "Tesla Ice Zhang" <ice1000kotlin at foxmail.com>
> Cc: "jan lahoda" <jan.lahoda at oracle.com>, "amber-dev" <amber-dev at openjdk.java.net>
> Sent: Samedi 25 Septembre 2021 20:10:24
> Subject: Re: Pattern Matching for switch (Second Preview)

>> It's not exactly the same semantics because "Object e" allows null while
>> "default" don't.
> 
> The semantics more more different than that; because default has special
> prvileges with respect to ordering.  Bottom line: don't use default in
> pattern switch, use real patterns.

yes very true,
I would love to have javac to emit a warning if default is not the last case (for all switches).

> 
>>
>>> But I like the default syntax since it can be put to the beginning of the switch
>>> expression, and the type of `e` may not always be Object.
>> You will be able to use "var" instead of "Object" soon.
> 
> Will you ?  News to me.

It's hard to argue that Object o in "case Object o" and case Box(Object o)" should have the same semantics and at the same time allow "case Box(var o)" but do not allow "case var o".

Rémi


More information about the amber-dev mailing list