Switch expressions -- gathering the threads

Stuart Marks stuart.marks at oracle.com
Mon Apr 16 23:59:20 UTC 2018



On 4/10/18 12:34 PM, Dan Smith wrote:
>>      int x = switch (y) {
>>          case "Foo" -> 1;
>>          case "Bar" -> 2;
>>
>>          case null:
>>          default:
>>              // handle exceptional case here
>>      }
>>
>> ... 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.

Although "fallthrough" isn't formally defined anywhere, as far as I can see, 
pragmatically I agree this is a useful distinction to make. I'll note that javac 
does distinguish these cases when -Xlint:fallthrough is enabled. Specifically, 
javac will not issue a warning when compiling this construct:

         switch (x) {
             case 1:
             case 2:
                 break;
         }

but it WILL issue a warning when compiling this construct:

         switch (x) {
             case 1:
                 ;
             case 2:
                 break;
         }

     warning: [fallthrough] possible fall-through into case

The rule seems to be, if one or more statements appear between case labels, and 
the last of those statements isn't a break (or some other non-local control 
construct such as return, throw, or continue), then a warning is issued.

s'marks


More information about the amber-spec-observers mailing list