Would the suggestion to have a syntax for a switch
that does not need breaks
all the time feasible?
My suggestion would be to insert auto
both before switch
and each case:
auto switch(expr) {
auto case 1: statement; statement;
auto case 2: statement; statement;
default: statement; statement;
}
would be equivalent to
switch(expr) {
case 1: statement; statement; break;
case 2: statement; statement; break;
default: statement; statement;
}
The compiler would complain with an error
- if in a
auto switch
anycase
is not prepended withauto
or - if a
auto case
is used in anauto
-lessswitch
.
Is there any parser problem to be expected, any ambiguities?
Is there additional benefit from less wrong-use-errors of users, like better optimization possibilities?
Update
A slightly more useful case would be where the location of the break is not so clear:
case(expr) {
case 1:
if(isThisTrue) {
print(that);
} else {
break;
}
doSomethingElse();
if(!isThisTrue) {
break;
}
...or something like this.
I think prevention of fall-through is useful to prevent some difficult to diagnose errors.