Suggestion: new switch statement

Kristian kjkilpi at gmail.com
Mon Aug 21 02:00:47 PDT 2006


It would be nice if there was a new statement that acts like 'switch'  
except that 'break' will be automatically added at the end of the case  
statements. (We don't want to change the behavior of 'switch' because  
it'll break old code and confuse people familiar with C/C++.)

Let call this new statement 'jump' (or something), for example:

void func(int val) {
     jump(val) {
         case 1:
             doX();
         case 2, 3:
             doY();
         default:
             doZ();
     }
}

Using 'switch', 'func()' would look, of course, as follows:

void func(int val) {
     switch(val) {
         case 1:
             doX();
             break;
         case 2, 3:
             doY();
             break;
         default:
             doZ();
             break;
     }
}

You could use a 'unbreak' (or something) to allow execution to fall  
through to the next case statement.

In addition, falling through should be allowed in the following case:

     case 1:
     case 2:

which is a longer way to write "case 1, 2:", of course.


I think breaking after a case is (a lot) more common than falling through.  
So this will reduce bugs (a programmer can easily forget to put the break  
statement at the end of a case). Also, it'll make code shorter and easier  
to read.



More information about the Digitalmars-d mailing list