www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - switch with regex

reply BCS <BCS_member pathlink.com> writes:
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;
}
Feb 25 2006
next sibling parent reply kellywilson nowhere.com writes:
In article <dtqosg$2l1q$1 digitaldaemon.com>, BCS says...

switch(str)
{
case "[Tt]his"~~:
writef("Doing this");
break;

case "[Tt]hat"~~:
writef("Doing this");

I'm guessing this "writef" statement is supposed to say "Doing That"?????? Just noticed it quickly ;) Thanks, K.Wilson
Feb 25 2006
parent reply Tyro <ridimz_at yahoo.dot.com> writes:
kellywilson nowhere.com wrote:
 In article <dtqosg$2l1q$1 digitaldaemon.com>, BCS says...
 
 I'm guessing this "writef" statement is supposed to say "Doing That"??????
 
 Just noticed it quickly ;)
 
 Thanks,
 K.Wilson
 

I seriously doubt that is what he is asking and it happens that your intuition has misled. The switch does not work because the language does not support kind of feature he is trying to use. Thus: He is suggesting that this should be implemented and wants to know what whether or not you think it's a good idea. Regards, Andrew C. Edwards
Feb 25 2006
parent reply kellywilson nowhere.com writes:
In article <dtqs7a$2onk$1 digitaldaemon.com>, Tyro says...
kellywilson nowhere.com wrote:
 In article <dtqosg$2l1q$1 digitaldaemon.com>, BCS says...
 
 I'm guessing this "writef" statement is supposed to say "Doing That"??????
 
 Just noticed it quickly ;)
 
 Thanks,
 K.Wilson
 

I seriously doubt that is what he is asking and it happens that your intuition has misled. The switch does not work because the language does not support kind of feature he is trying to use. Thus: He is suggesting that this should be implemented and wants to know what whether or not you think it's a good idea. Regards, Andrew C. Edwards

Hey Andrew, This wasn't actually a serious answer from me....more of a quick prod to let him know there was a slight error. I didn't know enough about the regex/switch syntax in D to answer his question right away. Sorry to mislead, if I did. Kelly Wilson
Feb 26 2006
parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
kellywilson nowhere.com wrote:
 In article <dtqs7a$2onk$1 digitaldaemon.com>, Tyro says...
 
kellywilson nowhere.com wrote:

In article <dtqosg$2l1q$1 digitaldaemon.com>, BCS says...

I'm guessing this "writef" statement is supposed to say "Doing That"??????

Just noticed it quickly ;)

Thanks,
K.Wilson

I seriously doubt that is what he is asking and it happens that your intuition has misled. The switch does not work because the language does not support kind of feature he is trying to use. Thus: He is suggesting that this should be implemented and wants to know what whether or not you think it's a good idea. Regards, Andrew C. Edwards

Hey Andrew, This wasn't actually a serious answer from me....more of a quick prod to let him know there was a slight error. I didn't know enough about the regex/switch syntax in D to answer his question right away. Sorry to mislead, if I did. Kelly Wilson

The match expression is now gone anyway!! Also I suspect that there's no performance advantage; there can't be.
Feb 26 2006
prev sibling next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Personally, I'd imagine that to be very strange.  You can't use any 
other operator like that in a switch, can you?

However, allowing for other comparisons might be very useful, e.g.:

switch (x, opCmp)
{
...
}

Or:

switch (x, std.regexp.test)
{
...
}

Unfortunately, this isn't very consistent with the rest of the language 
either, since it does not take callbacks like that anywhere else.

-[Unknown]


 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;
 }
 
 

Feb 25 2006
prev sibling parent John Demme <me teqdruid.com> writes:
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;
 }

Feb 25 2006