Switch expressions -- gathering the threads

Dan Smith daniel.smith at oracle.com
Tue Apr 10 19:34:13 UTC 2018


> On Apr 9, 2018, at 1:14 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> 
> A form of fallthrough that I think may be more common in expression switches is when something wants to fall _into_ the default:
> 
>     int x = switch (y) {
>         case "Foo" -> 1;
>         case "Bar" -> 2;
> 
>         case null:
>         default:
>             // handle exceptional case here
>     }
> 
> Because `default` is not a pattern, we can't say:
> 
>     case null, default:
> 
> here.  (Well, we could make it one.)  Though we could carve out an exception for such "trivial" fallthrough.

As a matter of terminology, I think it would be helpful for us to not call this fallthrough at all. It creates a lot of confusion when somebody is making an assertion about fallthrough, and it's unclear whether this kind of thing is being included or not.

JLS is a good guide: grammatically, the body of a switch statement is a sequence of SwitchBlocks, each of which has a sequence of SwitchLabels followed by some BlockStatements.

https://docs.oracle.com/javase/specs/jls/se10/html/jls-14.html#jls-14.11 <https://docs.oracle.com/javase/specs/jls/se10/html/jls-14.html#jls-14.11>

JLS doesn't formally define the concept of "fallthrough" but I suggest we use it to describe the situation in which control flows from one SwitchBlock to another.

What you've illustrated is instead a "switch case with multiple labels"—something deserving scrutiny on its own, but really a different sort of problem than fallthrough.

—Dan


More information about the amber-spec-observers mailing list