JEP325: Switch expressions spec

Guy Steele guy.steele at oracle.com
Mon Apr 23 19:00:55 UTC 2018


> On Apr 23, 2018, at 2:58 PM, Kevin Bourrillion <kevinb at google.com> wrote:
> 
> On Mon, Apr 23, 2018 at 11:20 AM, Guy Steele <guy.steele at oracle.com <mailto:guy.steele at oracle.com>> wrote:
> 
> (1) We have moved toward allowing “arrow versus colon” to be a syntax choice that is COMPLETELY orthogonal to other choices about the use of `switch`.  If this rule is to hold universally, then any switch statement or expression should be convertible between the arrow form and the colon form using a simple, uniform rule.
> 
> (2) In switch expressions we want to be able to use the concise notation `case a, b, c -> s;` for a switch clause.
> 
> (3) From (1) and (2) we inexorably conclude that `case a, b, c: break s;` must also be a valid syntax.
> 
> (4) But we could also have written (3) as `case a: case b: case c: break s;` and we certainly expect them to have equivalent behavior.
> 
> (5) From (1) and (4) we conclude that we ought also be to be able to write `case a -> case b -> case c -> s;`.
> 
> Not necessarily, if one simply views (4) as being an artifact of colonform switch's capacity for fall-through, which we know should not carry over. (Although we technically don't use the term "fall-through" in this no-intervening-code case, it works the same way and many people do think of it that way.)

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.

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.

—Guy
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20180423/7d49c329/attachment.html>


More information about the amber-spec-experts mailing list