john.r.rose at oracle.com
Sat Mar 6 02:14:08 UTC 2021
On Mar 5, 2021, at 3:08 PM, John Rose <john.r.rose at oracle.com> wrote:
> s/when/&&/ is my proposal; then &&guard is the visual cue,
> and you can stack them. (If you want || or ?:, you will need
> to use parens.)
The grammatical way to say this is that a switch case pattern can be optionally followed by && and then a ConditionalAndExpression whose form can’t be a||b but can be a&&b.
An instanceof pattern has no such rule because as an expression it can be naturally followed by &&a which takes the role of a guard in that case.
This makes me wonder: What happens when you stick &&a&b after a case? Well, that’s pretty simple; a and b must be boolean expressions.
Here’s a better one: What if you write x instanceof T t & q? Of course q is an expression. If you dress up q to resemble a pattern you might have a nice puzzler. So how do I write it if I really want two patterns? Maybe we enlist parentheses?
I saved the best for last. What does this do?
x instanceof Integer t & t > 0
The answer indicates that users of patterns will have to be on their toes about the difference between & and &&. This makes me feel more confident about using bot & and && for syntax distinctions, as an approximation to overloading simple & for both patterns and guards. && is really a pattern breaker whereas & is a pattern continuer.
Answer to puzzler:t can only be flow-scoped after && not &.
More information about the amber-spec-experts