digitalmars.D - switch with regex
- BCS (20/20) Feb 25 2006 Thoughts please:
- kellywilson nowhere.com (5/12) Feb 25 2006 I'm guessing this "writef" statement is supposed to say "Doing That"????...
- Tyro (8/17) Feb 25 2006 I seriously doubt that is what he is asking and it happens that your
- kellywilson nowhere.com (7/24) Feb 26 2006 Hey Andrew,
- Hasan Aljudy (3/37) Feb 26 2006 The match expression is now gone anyway!!
- Unknown W. Brackets (15/44) Feb 25 2006 Personally, I'd imagine that to be very strange. You can't use any
- John Demme (19/46) Feb 25 2006 In my mind, a switch-case implies two things: first, that one case will
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
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
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.WilsonI 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
In article <dtqs7a$2onk$1 digitaldaemon.com>, Tyro says...kellywilson nowhere.com wrote: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 WilsonIn 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.WilsonI 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 26 2006
kellywilson nowhere.com wrote:In article <dtqs7a$2onk$1 digitaldaemon.com>, Tyro says...The match expression is now gone anyway!! Also I suspect that there's no performance advantage; there can't be.kellywilson nowhere.com wrote: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 WilsonIn 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.WilsonI 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 26 2006
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
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