Call for feedback -- enhanced switch
Jorn Vernee
jbvernee at xs4all.nl
Sun Mar 24 16:17:28 UTC 2019
Hi,
Tried this out with some of the Panama code (did not get the build
working with --enable-preview though).
Some observations;
* for 49 switch statements, ~630 lines were dropped with the refactor.
* Pretty much any switch statement benefits from a refactor to the '->'
style, because of the ability to drop 'break' or 'return' from branches.
* Ability to merge multiple 'case XYZ:' is really nice when having lots
of fall-through. This brings forward the code in the branch more, and
not just the labels.
* Expression switch requires a closing ';'. This is pretty annoying, and
doesn't feel like it adds anything.
* Exhaustive expression switch for enums is nice, but you have to be
careful about not removing the default case when not using an expression
switch (or when changing code later). This feels like a source of bugs,
but maybe it just takes some getting used to. (Probably it would be nice
if the IDE showed some marker when an automatic default case is
present).
* `break value;` seems fine to me. This feels analogous to `return x;`
vs. `return;`. No need for `break-with` imho.
* Also, maybe a bug:
```
enum MyEnum {
X, Y;
}
public static void main(String[] args) throws Throwable {
int x = 5;
int y = 10;
MyEnum myEnum = MyEnum.X;
int o;
switch(myEnum) {
case X -> o = x;
case Y -> o = y;
// automatic exception
};
System.out.println(o);
}
```
Even though the switch should be exhaustive, javac complains that 'o'
might not have been initialized.
Cheers,
Jorn
Brian Goetz schreef op 2019-03-01 22:55:
> We would like to promote enhanced switch to a permanent feature in
> Java 13. So far, the only change we are anticipating is:
>
> - change “break value” to “break-with value”.
>
> This is the time for people to try it out on their codebases (there’s
> even refactoring support in IDEA!) and provide feedback on what works,
> what doesn’t, and what was surprising.
More information about the amber-dev
mailing list