www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14795] New: Extending D's switch statement

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 puremagic.com
          Reporter: yshuiv7 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.

--
Jul 12 2015