Extend D's switch statement?

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 8 10:30:58 PDT 2015


On 07/08/2015 10:31 AM, Martin Nowak wrote:
> On Wednesday, 8 July 2015 at 07:15:27 UTC, Yuxuan Shui wrote:
>> I think it will be useful to extent switch statement to support any
>> type that implements opEquals and opHash.
>>
>> Is there any technical difficulties to implement this?
>
> Hardly anyone wants switch to be a better if-else ladder.
> https://issues.dlang.org/show_bug.cgi?id=5862

I don't think this is what is being asked for.

switch(x){
     case a:
         ...;
         break;
     case b:
         ...;
         break;
     default: break;
}

->

switch(x.toHash()){
     case a.toHash():
         if(x!=a) goto default;
         ...;
         break;
     case b.toHash():
         if(x!=b) goto default;
         ...;
         break;
     default: break;
}

(a and b are known at compile time here, and so are their hash values. 
Hash collisions between e.g. a and b would need to be dealt with of course.)


More information about the Digitalmars-d mailing list