The Wrong Stuff

bearophile bearophileHUGS at lycos.com
Fri Sep 24 14:11:21 PDT 2010


Jonathan M Davis:

> Indeed, though it might be okay to allow totally empty case statements on the 
> theory that the programmer pretty much couldn't have meant anything other than 
> fallthrough - though given that D has the syntax to do that as one case 
> statement, it might be reasonable (and probably easier to implement) to not 
> allow totally empty case statements.

In my opinion empty case fall-through is not significantly bug-prone, so it may be acceptable:

switch (x) {
    case 1:
    case 2:
    case 3: x++; break;
    default: y++; break;
}

But it has few disadvantages:

In most cases in D2 it's not necessary, because D allows the comma operator to list several cases, and it has the range case syntax:

switch (x) {
    case 1, 2, 3: x++; break;
    default: y++; break;
}

switch (x) {
    case 1: .. case 3: x++; break;
    default: y++; break;
}


It adds a corner case to a rule. The rule is: "D2 doesn't allow 'free' fall-through, some control statement is required". If that is allowed then you need "D2 doesn't allow 'free' fall-through, some control statement is required, unless the case statement is empty."

Adding one corner case doesn't make the language significantly worse, but many corner cases make the language more complex for the programmer, the D2 compiler, and the D2 manuals. So corner cases need to be added only when they are very useful.

On this topic I have a bug report:
http://d.puremagic.com/issues/show_bug.cgi?id=4349

Bye,
bearophile


More information about the Digitalmars-d mailing list