[patterns] Nullability in patterns, and pattern-aware constructs (again)

Brian Goetz brian.goetz at oracle.com
Thu Jan 9 15:05:20 UTC 2020

> I'm just thinking if such arrangement allows expressing all possible 
> situations that may arise in a switch.
> Imagine one wants to switch on (Object o) and have the following cases:
> - String: case 1
> - CharSequence or null: case 2
> - any other: case 3
> How would one structure such switch? Would case 2 have to be split 
> before and after case 1?

Probably, but there are still some open questions that might affect the 
answer.    Note this is not all that different from:

  - String: case 1
  - CharSequence, or the constant string "Foo": case 2
  - any other: case 3

The goal is not necessarily to be able to represent any chain of 
conditionals in switch, though of course we do not want to gratuitously 
make it difficult.

As I mentioned in the "lots of lousy ways to say T or null" section, we 
can make this easier at the cost of greater complexity.  Type patterns 
have a binding variable; constant patterns (currently) do not.  If they 
did, you could say:

     case String s: ...
     case null x, CharSequence x: ...
     case Object: ...

But, this brings two kinds of complexity with it: binding merging, and 
constant patterns with binding variables, which are (mostly) useless 
except for corner cases like this.

More information about the amber-spec-experts mailing list