What is the use case for this weird switch mecanism

Era Scarecrow rtcvb32 at yahoo.com
Tue Oct 30 14:51:39 PDT 2012


On Tuesday, 30 October 2012 at 21:40:26 UTC, bearophile wrote:
> Era Scarecrow:
>> Maybe variable declaration (as long as they are default(s))?
> Declaring variables there is dangerous:

>     switch(bar) {
>         Foo f;
>         case 10: break;
>         default: writeln(f); // prints garbage
>     }

  Then it's as though it were '= void;' by default. Most curious. 
Honestly I'd say it's illegal to have something before any 
callable case; Besides for goto's it's illegal to jump past 
declarations, yet this switch case allows it.

  I'd say one of two things must happen then.

   1) Code before the first case is disallowed
   2) Code before the first case always runs

  Option 2 seems silly and unneeded, except it allows a small 
scope during the switch call, which is it's only possible 
advantage. The only other advantage is you could have a case 
disabled and enable it during certain debugging cases, but in 
those cases why not do the whole block?

>     switch(bar) {
           static if (DEBUG) {
               case -10: /*disabled case, or something like that*/
           }
           Foo f;
>         case 10: break;



More information about the Digitalmars-d mailing list