[Issue 14795] New: Extending D's switch statement
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sun Jul 12 02:21:20 PDT 2015
https://issues.dlang.org/show_bug.cgi?id=14795
Issue ID: 14795
Summary: Extending D's switch statement
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: yshuiv7 at gmail.com
Can we extent switch statements to support (compile-time known) values with
type other than {,w,d}char[] or int?
I suggest we can support any types that implement toHash and opEquals. Switch
statements like this:
switch(a) {
case a1:
break;
case a2:
break;
case a3:
break;
...
}
are thus lowered to:
switch(a.toHash) {
case a1.toHash:
if (a1 == a) {...}
break;
case a2.toHash:
if (a2 == a) {...}
break;
case a3.toHash:
if (a3 == a) {...}
break;
...
}
Hash collisions can be resolved into an if-ladder.
--
More information about the Digitalmars-d-bugs
mailing list