Pattern matching for switch (Third Preview)
Brian Goetz
brian.goetz at oracle.com
Wed Mar 23 19:02:18 UTC 2022
> 0. I noticed a `<>` operator, is that a typo?
Likely a rendering artifact; the markdown renderer in the bug system is
different from that of the OpenJDK website, which ultimately is the
definitive view.
> 1. What's the motivation of replacing `??` with a new soft keyword? What's bad about `??`?
This was discussed on the amber-spec-experts list in January; see
threads rooted at "reviewing feedback on patterns in switch". The key
part of this change is not the syntactic change from && to `when`, but
moving the refinement from the pattern itself, to the switch. The
change in syntax follows naturally from this change in location.
FWIW, adjoining guards to patterns has some nasty surprises lurking,
which we knew about but convinced ourselves we could ignore at first.
For example, if we ever have switches over boolean (which we surely
will, as pattern matching has been a forcing function for regularizing
all aspects of switch, and this one will surely come), you can then get
some bizarre combinations, which don't mean what you think they mean, like:
switch (aBoolean) {
case false && false: // will never be selected!
...
}
In the design discussion for this feature, we went back and forth
several times between refining patterns and refining switch cases, since
there were pros and cons of each. In the end, it felt like refinement
fit more cleanly in the conditional construct (switch) than in the
pattern machinery. This is why we do previews; to give decisions a
chance to settle.
More information about the amber-dev
mailing list