Rehabilitating switch -- a scorecard

Brian Goetz brian.goetz at oracle.com
Tue May 18 02:37:55 UTC 2021


> There are a few roads not taken:  “switch ()” with boolean
> case expressions has not showed itself worthy yet.

Yep, this one can sit on the shelf.

> I’d also like to point out “switch (a, b)” as a possible area
> of future work for switch, where the thing after “switch” is a
> more generalized argument expression

ML supports this, because it just treats the operand as a tuple, and 
automatically destructures tuples.   The killer use case is, of course, 
FizzBuzz:

     switch (n % 3, n % 5) {
         case (0, 0) -> "FizzBuzz";
         case (0, _) -> "Fizz";
         case (_, 0) -> "Buzz";
         default -> n.toString();
     }


> 1. Allow certain simple statements after “->” without
> requiring “{ … }” wrappers.

We already do this with `throw`, but could extend.   I'm waiting for 
Valhalla to make `void` a type, and then see how painful it is to merge 
statement switch with void expression switch.  (Now that we've 
totalized, it's easier.)

> 2. Allow a way to add a given case to the “remainder”
> set, to be treated similarly to “built-in” remainder
> processing.  Proposed syntax:  “throw;” as in
> “case null->throw;”.  (Raises the question of whether
> to support such a notation outside of switch,
> but we could just say “no”.  Or spell it “throw default;”
> or “throw assert”.)

There's two aspects of this: matching on the remainder (case else?), and 
denoting the default throw.




More information about the amber-spec-experts mailing list