Another switch suggestion.

Fredrik Olsson peylow at gmail.com
Thu Aug 31 10:37:13 PDT 2006


As the switch syntax already allows for more funky stuff than C does, 
that requires some kind of code to be inserted why not extend on it a 
bit and allow for say:

switch (foo) {
   case ($ >= 'a' && $ <= 'z') || ($ >= 'A' && $ <= 'Z'):
     doStuff();
     break;
   default:
     doOtherStuff();
     break;
}


Just as $ means "length of array" for slicing an array and such, why not 
let $ mean "the value the current switch is testing for"? And if the 
case have a boolean expression instead of a constant than the first case 
that evaluates to true should be run.

One of the few features I miss from Visual Basic :).

And an even better syntax for the above example would naturally be:
switch (foo) {
   case $ in <'a'..'z', 'A'..'Z'>:
     doStuff();
     break;
   default:
     doOtherStuff();
     break;
}


// Fredrik Olsson



More information about the Digitalmars-d mailing list