Suggestion: new switch statement

Chad J gamerChad at _spamIsBad_gmail.com
Mon Aug 21 18:56:40 PDT 2006


Kristian wrote:
> 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.

Creating a new type of switch statement just to change some default
behavior seems a bit excessive.

At the same time, I can't stand the fall-through default behavior!  It
is a source of bugs, and I've already been hit by it a few times in D
and spent hours trying to find the bug in a switch statement that
behaves subtly different than what I expected.

I've read that this is maintained in D to keep it easy to port C code
over to D.  That's understandable, especially if you want to make break
the default, which would change the meaning of code without any warning.

I'd suggest though, that you don't allow a default ending for case
blocks at all.  Mandate that each case block be followed by an escape
like 'break', 'return', 'goto', 'throw', etc.  That way, the compiler
would throw an error when C code that uses fallthroughs is inadvertently
used as D code.  Also, this is one of those things from C/C++ that is
bad.  D is supposed to get rid of those bad things even if it means
losing backwards compatibility, so please get rid of this bad thing that
comes from C.

I also like Mike's suggestion on this thread a lot.  It could even be 
combined with my suggestion.  I'd say use Mike's suggestion, and only 
apply my suggestion to the colon blocks.  Since the brackets and 
one-liners are not valid C syntax AFAIK, the compiler won't need to 
mandate anything about them to flush out bugs from incoming C code.  On 
the developer side, this trades nasty runtime bugs for some small amount 
of hunting down compiler errors and fixing them.  A good trade IMO.  Not 
sure what the cost would be to compiler writers like Walter, but please 
at least implement the mandatory ending on the current case blocks, or 
something just as good.



More information about the Digitalmars-d mailing list