3 ms·
The "C" family has one of the worst case/switch statements out there. When Java et. al. cloned it, they should have provided an alternative. Perhaps keep the ol
by tabtab 3y ago
The "C" family has one of the worst case/switch statements out there. When Java et. al. cloned it, they should have provided an alternative. Perhaps keep the old style for backward compatibility, but provide a new one with different key-words to avoid confusion. Example:
select(a) {
when 1,2,3 {...}
when 4 {...}
when 5,6 {...}
otherwise {...}
}
- twic 3y agoAgreed 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.