3 ms·
The "&&" approach is how you express that same logic in an if statement in Java today. Given this pattern matching syntax change, you'd write: switch(obj)
by garblegarble 4y ago
The "&&" approach is how you express that same logic in an if statement in Java today.
Given this pattern matching syntax change, you'd write:
switch(obj) {
case Tuner t when guitar.isInTune() -> ...;
...
}
For a switch, but for an if statement it's written as:
if (obj instanceof Tuner t && guitar.isInTune()) {
...
}
Edit: I do wonder, actually, if avoiding "&&" is to allow the "when" case execution to be reordered (e.g. to allow JIT to extract common "when" conditions prior to the switch expression), which would be wrong to do given the short-circuiting rule implications of "&&"