Suggestion: new switch statement

Kristian kjkilpi at gmail.com
Wed Aug 23 06:09:26 PDT 2006


On Tue, 22 Aug 2006 18:55:29 +0300, mike <vertex at gmx.at> wrote:
> Hi!
>
> Have you ever tried to use the thumb for alt-gr and pressing 7/0 with  
> the middle finger?

I do that also sometimes. Pressing AltGr+7 with the middle finger happens,  
um, maybe 10-20% of the cases, at maximum, though. Pressing AltGr with a  
right hand's thumb and 7 with the left hand's middle finger does not  
involve lifting my right elbow. Which seems to happen when using one hand  
only... Hey, that must be a reason why I have adopted the two hand  
style... :)


>
> Anyway, I thought a bit about that.
> I'd really like the switch to look like that:
>
> ' switch (bar)
> ' {
> '     case (1) writefln("case 1");
> '     case (2, 3)
> '     {
> '         writefln("case 1");
> '         writefln("or case 3");
> '     }
> '     case (4)
> '     {
> '         writefln("case 4 - need to fall through to 5");
> '         continue(5);                           // continue is used  
> instead of falling through
> '     }
> '     case (5)
> '     {
> '         writefln("case 5 - checking for foo");
> '         if (!foo()) break;                     // breaks out of the  
> switch
> '         writefln("foo is true");
> '     }
> '     default
> '     {
> '         writefln("default");
> '         throw new Exception("Don't do that!");
> '     }
> ' }
>
> With the parenthesis and the continue. But if you try to mix that with  
> the current switch syntax, like
>
> ' switch (bar)
> ' {
> '     case (1) writefln("case 1");
> '     case 2: evil_flag = true; // fall through
> '     case (3) if (evil_flag) { ... }
> ' }
>
> it's getting ugly. What about allowing my proposed syntax and aborting  
> compilation when one tries to mix C-style switches with what I have in  
> mind?
>
> -Mike

Yeah, mixing the different syntaxes could make a mess. I guess compiler  
could produce error messages in such situations. Importantly it wouldn't  
break old code.

Now this syntax will indeed fit to the overall syntax ("if() {}" <=>  
"case() {}"). When I read my first book of C I wondered why the syntax of  
the switch statement was different and not like this one. I guess that's  
because it will allow you to write some complex code. But this hybrid  
style would allow that also.



More information about the Digitalmars-d mailing list