Exhaustiveness in switch

Brian Goetz brian.goetz at oracle.com
Thu May 10 22:52:40 UTC 2018


OK, but consider these cases.

Case 1:
     enum E { A, B }
     ...

     switch (e) {
         case A -> ...
         case B -> ...
         default: throw ...;
     }

Now, we add "C" to E and recompile.  No compilation error, and it throws 
on input C.

Case 2:

     enum E { A, B }
     ...

     switch (e) {
         case A -> ...
         case B -> ...
         // implicit throwing default
     }

Again, we add C and recompile.  Now we get a compilation error, because 
the switch isn't exhaustive.

Case 3: compiler doesn't try to reason about exhaustiveness of enums.

     enum E { A, B }
     ...

     switch (e) {
         case A -> ...
         case B -> ...
     }

And we get a compilation error because there's no default.

I was under the impression you were a big fan of (2), because then the 
compiler notifies you when your switch becomes non-exhaustive.  With an 
explicit default (1), or no implicit throwing default (3), you lose that.

Can you clarify?

On 5/10/2018 5:05 PM, Kevin Bourrillion wrote:
> The only distinction I am discussing now is whether it is implicit or 
> explicit.  It was never my intention to argue that the benefits came 
> from the implicitness - I just want there to be a way to choose 
> between the two behaviors.
>



More information about the amber-spec-experts mailing list