> On Dec 14, 2017, at 4:22 PM, Brian Goetz <[email protected]> wrote: > > . . . > > So we dusted off an old idea, which we'd previously explored but which had > some challenges, which is to use "break" with an operand instead of "return" > to indicate local return in switch expressions. So: > > int y = switch(abs(x)) { > case 1 -> 1; > case 2 -> 2; > case 3 -> 3; > default -> { > println("bigger than 3"); > break x; > } > }; > > The challenge is ambiguity; this could be interpreted as a nonlocal break out > of an enclosing loop whose label is `x`. But then we realized that if `x` is > both a variable and a label, we can just reject this, and tell the user to > rename one or the other; since alpha-renaming the label is always source- and > binary-compatible, the user has at least one (if not two) reasonable choices > to get out of this problem. . . .
By all means, reject the ambiguous situation; this sounds fine. But also advise the user of another possibility: if the intent is to return the value of the variable `x` (rather than use the label), just use parentheses and write `break (x);`. Which I might take to doing in all cases anyway, just to make it _immediately_ clear even in nonambiguous cases that `x` is not a label. —Guy
