Reviewing feedback on patterns in switch
Brian Goetz
brian.goetz at oracle.com
Tue Feb 15 18:50:06 UTC 2022
We're preparing a third preview of type patterns in switch. Normally we
would release after a second preview, but (a) we're about to get record
patterns, which may disclose additional issues with switch, so best to
keep it open for at least another round, and (b) we're proposing some
nontrivial changes which deserve another preview.
Here's where we are on these.
> 1. Treatment of total patterns in switch / instanceof
Quite honestly, in hindsight, I don't know why we didn't see this
sooner; the incremental evolution proposed here is more principled than
where we were in the previous round; now the construct (instanceof,
switch, etc) *always* gets first crack at enforcing its nullity (and
exception) opinions, and *then* delegates to the matching semantics of
the pattern if it decides to do so. This fully separates pattern
semantics from conditional construct semantics, rather than complecting
them (which in turn deprived users of seeing the model more clearly.)
In hindsight, this is a no-brainer (which is why we preview things.)
We'll be addressing this in the 3rd preview.
> 2. Positioning of guards
Making guards part of switch also feels like a better factoring than
making them part of patterns; it simplifies patterns and totality, and
puts switch on a more equal footing with our other conditional
constructs. We did go back and forth a few times on this, but having
given this a few weeks to settle, I'm pretty convinced we'd regret going
the other way.
There were two sub-points here: (a) is the guard part of the pattern or
part of switch, and (b) the syntax. There was general agreement on (a),
but some had preference for && on (b). I spent some more time thinking
about this choice, and have come down firmly on the `when` side of the
house as a result for a number of reasons.
- Possibility for ambiguity. If switching over booleans (which we
will surely eventually be forced into), locutions like `case false &&
false` will be very confusing; it's pure puzzler territory.
- && has a stronger precedence than keyword-based operators like
`instanceof`'; we want guards to be weakest here.
- Using && will confuse users about whether it is part of the
expression, or part of the switch statement. If we're deciding it is
part of the switch, this should be clear, and a `when` clause makes that
clear.
- There are future constructs that may take patterns, and may (or may
not) want to express guard-like behavior, such as `let` statements
(e.g., let .. when .. else.) Expressing guards here with && is even
less evocative of "guard condition" than it is with switches.
- Users coming from other languages will find `case...when` quite clear.
- We've talked about "targetless" switches as a possible future
feature, which express multi-way conditionals:
switch {
case when (today() == TUESDAY): ...
case when (location() == GREENLAND): ...
...
}
This would look quite silly with &&. Similarly, one could mix guards
with a targeted switch:
switch (x) {
case Time t: ...
case Place p: ...
default when (today() == TUESDAY): ... tuesday-specific default
default: ... regular default ...
Expressing guards that are the whole condition with `when` is much more
natural than with &&.
tl;dr: inventing a `when` modifier on switch now will save us from
having to invent something else in the future; choosing && will not.
We can continue to discuss the bikeshed at low volume (at least until we
start repeating ourselves), but we need to address both of these points
in the 3rd preview.
> 3. Type refinements for GADTs
I've been working through the details here, and there are a number of
additional touch points where GADTs can provide type refinement, not
just on the RHS of a case, such as totality and inference. I'll be
pulling all these together to try to get a total picture here. It's not
a blocker for the 3rd preview, it can be a future refinement.
> 4. Diamond for type patterns (and record patterns)
This seems desirable, but there are details to work out. It's not a
blocker for the 3rd preview, it can be a future refinement.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20220215/be705c60/attachment.htm>
More information about the amber-spec-experts
mailing list