JEP325: Switch expressions spec

Guy Steele guy.steele at
Fri Apr 20 18:55:56 UTC 2018

You know, if s2 is short (say, less than 30 or 40 characters), there are worse things than writing

         case A -> s1;
         case null -> s2;
         default   -> s2;

especially if you use spaces (as I just did) to line up the two occurrences of s2 to make it easy to see they are identical.

And if s2 is long, there are worse things than making a little sub method to handle it:

         case A -> s1;
         case null -> frobboz(a, b);
         default   -> frobboz(a, b);

    int frobboz(int a, String b) { … }

And if even THAT is not satisfactory, well, there are worse things than giving up on the arrows and just using colons (and break, if needed).

Yeah, null makes things uglier, but at least you have your choice of three different kinds of ugly.

BUT, on the other hand, if we wanted to: instead of, or in addition to,

        case pat1, pat2, pat3 -> s;

we could allow the form

        case pat1 -> case pat2 -> case pat3 -> s;

which of course could be stacked vertically for visual graciousness and perspicuity:

        case pat1 ->
        case pat2 ->
        case pat3 -> s;

and such a format would clearly accommodate

        case A -> s1;
        case null ->
        default -> s2;

Con: Could look like a programming error (unintentionally omitted statement), but that’s also true for the colon forms already permitted.
Con: More verbose than the comma-separated form `case pat1, pat2, pat3 ->`, which may matter for smallish switch expressions.
Pro: Doesn’t stick `default` in a weird place, or otherwise make a special rule just to handle “default and null”.
Pro: The keyword `case` appears in front of EVERY individual pattern, making them easier to see.
Pro: Avoids possible confusion between `case a,b,c ->` and `case (a,b,c) ->`.

Motto: “It’s not fallthrough, it’s just a SwitchBlockStatementGroup.”

> On Apr 20, 2018, at 2:40 PM, Brian Goetz <brian.goetz at> wrote:
> One thing that is relevant to the short term is that now that we killed mixed labels, we'd have to have a way to say "case null or default" in arrow world.  The least stupid thing seems to be to allow default to be tacked on to a comma-separated case list as if it were a pattern: 
>     case A -> s1;
>     case null, default -> s2;
> since you can no longer say:
>     case A -> s1;
>     case null:
>     default:
>         s2;

More information about the amber-spec-observers mailing list