switch with regex
John Demme
me at teqdruid.com
Sat Feb 25 17:35:03 PST 2006
In my mind, a switch-case implies two things: first, that one case will
match, and two that the compiler might be able to implement it faster than
if-else blocks. Given:
uint i;
switch(i){
case 2: break;
case 5: break;
}
Only one case will be selected, so the compiler doesn't necessarily just
convert that block of code to the associated if-else if-else code-- there
may be a faster way to execute it. The only way for the regex cases to be
compared is linearly, so the switch is just a nicer-looking if-else block.
Also, multiple regex cases may match the string. The behavior here is not
obviously defined. Does it run all of the cases which match? Only the
first one?
I think the behavior here would be too different and possibly inconsistent
from the current switch-case behavior to be worth it for syntactic sugar.
~John Demme
BCS wrote:
> Thoughts please:
>
> pros?
>
> cons?
>
> why it won't work.
>
> -------------
> switch(str)
> {
> case "[Tt]his"~~:
> writef("Doing this");
> break;
>
> case "[Tt]hat"~~:
> writef("Doing this");
> break;
>
> case ".*somthing.*"~~:
> writef("Doing somthing");
> break;
>
> default:
> writef("Doing nothing");
> break;
> }
More information about the Digitalmars-d
mailing list