[switch] Status of implementation of switch expression

Jan Lahoda jan.lahoda at oracle.com
Tue Feb 27 20:16:45 UTC 2018


Hi,

Just a quick overview of what's sketched in the switch branch in the 
amber/amber repository. Most features described in the JEP [1] should be 
sketched out, namely:
-switch as an expression, with both case variants:
--case <constant> -> <value>;
--case <constant>: <statements> break <value>;
-type inference inside the switch expression works in some cases, but 
may be buggy.
-enhanced exhaustiveness check for enums (if all existing constants are 
covered, the "default" clause may be omitted, and javac will generate it 
automatically. The automatically generated one will throw an exception, 
although the form of the exception may need some improvements). This is 
for switch expressions only.
-switch over long, float, double
-switch over boolean, with exhaustiveness check enhanced to consider 
"case true: case false:" to be exhaustive and not require default 
(although the default is allowed at this time despite being moot - that 
may change). This is for both switch statements and expressions.
-for switch statements and expressions over reference types, "case null: 
<statements>" (or "case null -> <expression>" in case of switch 
expression) may be used, which will be invoked when the reference is null
-multiple labels (for both switch statement and expression)
-throw in the expression context in switch expression ("case 0 -> throw 
new IllegalStateException()").
-the switches are desugared using indy and 
java.lang.runtime.SwitchBootstraps when possible and needed. This is the 
default for (when the target supports it):
--switch over Strings and enums
--switch over float, long, double, boolean and their boxes
--switch over Integer, if there is "case null:" in the switch

Current (new) tests are in the following directories:
http://hg.openjdk.java.net/amber/amber/file/a6b463a66e0c/test/langtools/tools/javac/switchexpr
http://hg.openjdk.java.net/amber/amber/file/a6b463a66e0c/test/langtools/tools/javac/switchextra
http://hg.openjdk.java.net/amber/amber/file/a6b463a66e0c/test/langtools/tools/javac/switchnull

There are likely bugs in the implementation - bug reports are very welcome.

Thanks,
     Jan

[1] http://openjdk.java.net/jeps/325


More information about the amber-dev mailing list