[patterns] Null handling

John Rose john.r.rose at oracle.com
Sat Oct 14 22:48:41 UTC 2017


That's my understanding also.  Such a switch
needs a case that matches a null, not just a default.

Also, "case P:" matches a non-null value of type "W"
if W is the wrapper for a primitive type P, but won't
match a null.  (Note that "case P" doesn't match
null, but it also doesn't throw null.  The throwing
only happens after all cases are proven not to
match.

There are a couple of reasons to do nulls this way:

1. It's behaviorally compatible with legacy switch,
so we can rehabilitate an existing feature rather
than add a new similar-but-different feature.

2. Java has an "attitude" about nulls, that unless
you test for them explicitly you risk NPE.
Switch is no exception, and this "attitude"
scales nicely to switch-on-pattern.

— John

On Oct 14, 2017, at 3:21 AM, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> Apart if i've missed something, the implementation is not complete,
> null handling is explicit, i.e.
> 
> static void testSwitchNumber(Number i) {
>   switch(i) {
>   case 1: System.out.println("One");break;
>   case null:
>   default: System.out.println("Other");break;
>   }
> }
> 
> cheers,
> Rémi



More information about the amber-dev mailing list