3 ms·
Agreed that the C-style switch is crammed with misfeatures. But Java does now let you write this (haven't tried compiling this, may contain small errors):
by twic 4y ago
Agreed that the C-style switch is crammed with misfeatures. But Java does now let you write this (haven't tried compiling this, may contain small errors):
static String describe(int change) {
return switch (change) {
case 0 -> "unchanged";
case -1, 1 -> "mostly unchanged";
case Integer i && i > 0 -> "increased";
case Integer i -> "decreased";
}
}
No fallthrough, can mix constants and conditions (with syntax some will find clunky), and is an expression.