General Category > Ideas
Switch proposal
lerno:
So, current options:
(A) case 3 | for empty fallthrough, explicit fallthrough when it is non empty.
(B) | case 3 to indicate the previous case was fallthrough, regardless whether it was empty or not.
(C) case 3, for empty fallthrough, explicit fallthrough when it is non empty.
Types of "fallthrough" keyword:
(a) fallthrough
(b) goto next
(c) goto case
(d) goto case 4
(e) continue case
(f) next
(g) nextcase
bas:
Wow, that's a lot of posts in one day..
The case of
--- Code: ---case 3|
--- End code ---
looks quite nice, but it differs from the case where you have some code, but also a fall-through.
I want to simplify the language as much as possible, so I don't really want to change the current behavior too much. If you think
about the possible bugs that often occur is only that a develop forgets to add the break keyword. So the only needed option I
think is to add the fallthrough keyword and require that for all fallthroughs. So for example:
--- Code: ---case 1:
break;
case 2:
fallthrough;
case 3:
fallthrough:
default:
// ...
--- End code ---
or maybe even allow empty cases to fallthrough without keyword to make code with lots of fallthrough's better readable
--- Code: ---case 1:
break;
case 2: // allowed because empty case
case 3:
fallthrough;
default:
// ..
--- End code ---
lerno:
I think fallthrough automatically on empty is very inconsistent. Consider this, with impllcit break:
--- Code: ---func void printTest(i32 foo) {
switch (foo) {
case 1:
printf("A\n");
case 2:
printf("B\n");
default:
// Do nothing
}
}
printTest(1);
--- End code ---
Prints "A"
Now we decide to comment out printf("A\n");
--- Code: ---func void printTest(i32 foo) {
switch (foo) {
case 1:
// printf("A\n");
case 2:
printf("B\n");
default:
// Do nothing
}
}
printTest(1);
--- End code ---
Now this prints "B". Perhaps not what we expected... So I think case 1, is a better suggestion for empty fallthrough (version C)
bas:
Fallthrough is always automatic, except when you use break. So there is never an automatic break.
The compiler gives gives a warning/error if you don't use break/fallthrough in cases there is a code..
lerno:
Err... My proposal was to add implicit break.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version