How to complex switch?
KennyTM~
kennytm at gmail.com
Thu May 12 04:42:03 PDT 2011
On May 12, 11 19:13, Matthew Ong wrote:
> Hi All,
>
> Anyway to include this cool feature of switch with D in the near future?
Why the obsession with 'switch'? 'if' works fine.
>
> switch(str){
> // regexp
> case "abc", "def", "as+b?": s1(); break;
>
> case "za+", "wd?", "aaa": s2(); break;
>
> default: s3();
> }
Regex isn't even a built-in feature, why would a 'switch' should support it.
if (match(str, regex("abc|def|as+b?"))
s1();
else if (match(str, regex("za+|wd?|aaa"))
s2();
else
s3();
>
> switch (tag) {
> default: s3()
> case 0, 1, 2, 3: s1()
> case 4, 5, 6, 7: s2()
> }
>
Already possible (except you're missing the 'break;')
> switch (x := f();) { // missing switch expression means "true"
> case x< 0: return -x
> default: return x
> }
Please no. Why introduce yet another operator ':=' ?!
auto x = f();
if (x < 0)
return -x;
else
return x;
>
> switch (x){
> case x< y: f1()
> case x< z: f2()
> case x == 4: f3()
> case z+y: f4()
> }
Assume you don't mean to fall-through
if (x < y)
f1();
else if (x < z)
f2();
else if (x == 4)
f3();
else if (x == z+y)
f4();
More information about the Digitalmars-d
mailing list