Fw: JEP 455: Non-enhanced switch statements

Brian Goetz brian.goetz at oracle.com
Fri Oct 27 14:28:01 UTC 2023


>
> Unfortunately enum is that one case where compatibility dictates that 
> we can't be exhaustive, which is a little sad... but I think that's 
> the best we can do?
>

What we do for enums is do exhaustiveness checking based on the 
compile-time state of the world, and generate a synthetic default clause 
that deals with separately-compiled changes.  (Same with sealed types.)  
This works pretty well, as it lets us type-check a (new) switch like

     int wavelengthNm = switch (trafficLightColor) {
         case RED -> 700;
         case YELLOW -> 580;
         case GREEN -> 540;
     }

and not require a "silly" default, while offering runtime protection 
against novel colors showing up at runtime, and notifying the user at a 
subsequent compile if something unexpectedly changed.


More information about the amber-spec-experts mailing list