Expression switch - an alternate proposal
Brian Goetz
brian.goetz at oracle.com
Fri Apr 13 12:03:33 UTC 2018
Let me clarify some things here.
>> I also don't understand Rémy's remark that a traditional break inside a switch expression is disallowed.
In the same way that "break" is disallowed in a lambda. Obviously
lambdas can contain the keyword "break", but it must be contained
_within_ a construct that permits break. So break-in-while-in-lambda is
OK, but break at the top level of a lambda is not OK. Additionally,
labeled break and continue cannot pass control _through_ a lambda
expression or expression switch.
Similarly, bare or labeled break, continue, and return are not allowed
at the top level of an expression switch. There's a spec draft, posted
on amber-spec-experts, that covers this, and there's been discussion on
the EG list of how this would work.
>> What rules out
>>
>> case BAKED_POTATO:
>> Toppings t = new Toppings();
>> t.add(SOUR_CREAM);
>> t.add(CHEESE);
>> int count = 0;
>> while (true) {
>> if (count == 10) break; else count++;
>> t.add(BACON);
>> }
>> break t;
>
Here, the `break` in the while loop breaks out of the while loop. That's
fine; that's ordinary composition (you can put statements in a switch; a
while-loop is a statement; a while loop with a break in it is a
statement.). At the top level within the expression switch, the break
must take an expression.
The whole point here is _not_ to invent a new and different construct.
I know its very tempting to say "why not just invent a simpler construct
just for this", but this idea always crashes and burns.
More information about the amber-dev
mailing list