-
Notifications
You must be signed in to change notification settings - Fork 100
Closed
Labels
Description
When every case body is an assignment to the same variable, and there is a default case, then we can turn the whole thing into a switch expression in java 21.
BEFORE
String formatted = "initialValue";
switch (obj) {
case null -> formatted = "null";
case Integer i -> formatted = String.format("int %d", i);
case Long l -> formatted = String.format("long %d", l);
case Double d -> formatted = String.format("double %f", d);
case String s -> {
String str = "String";
formatted = String.format("%s %s", str, s);
}
default -> formatted = "unknown";
}
AFTER
String formatted = switch (obj) {
case null -> "null";
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> {
String str = "String";
yield String.format("%s %s", str, s);
}
default -> "unknown";
}
Bhadboiabtimtebeek
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done