JEP325: Switch expressions spec
guy.steele at oracle.com
Mon Apr 23 19:29:52 UTC 2018
> On Apr 23, 2018, at 3:00 PM, Guy Steele <guy.steele at oracle.com> wrote:
> . . .
> You could view it that way—but such a view is incorrect, going back to JLS1.
> I know that many people develop alternate “folk models” for how they think things work or ought to work, but sometimes such alternate models lead one astray. This is one such instance. This is why we have a spec.
> My argument does not rely on fallthrough; it relies on the notion of a case label being “associated with” a statement. JLS is quite clear on this.
Alex Buckley has kindly pointed out to me that the preceding remark is in error. I misread the wording in JLS1 14.9 (and JDK8 JLS 14.11), paragraph 3. A case label is “associated with” the containing `switch` statement, not the statement that follows it in the SwitchBlockStatementGroup.
I should have said:
My argument does not rely on fallthrough; it relies on the notion of executing all statements after the matching case label in the switch block. This execution of statements, once initiated, may then “fall through” labels, but the initiation of this execution (specifically, initiation of execution of the first such statement) does not fall through labels. JLS is quite clear on this.
This is also confirmed by the first sentence in that section: “The `switch` statement transfers control to one of several statements depending on the value of an expression.” Despite the fact that one might like to think of "transferring control to a case label” rather than to a statement, the official model is that control is transferred directly to a statement.
> To be more explicit about assumption (1) above: I propose the simple, uniform rule that the way to convert colon form to arrow form is to (a) replace every colon in a SwitchLabel with an arrow, then (b) add braces and “break” where necessary (and the rules for this depend on whether you are converting a statement switch or an expression switch). The point of this definition is that step (a) need not require any exceptions or special cases.
More information about the amber-spec-observers