Extend D's switch statement?

Martin Nowak via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 8 14:26:26 PDT 2015


On 07/08/2015 08:06 PM, Yuxuan Shui wrote:
>> 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.)
> 
> Yes, this is what I meant.

Very interesting. How would you deal with collisions?
You can already do some nice things during compile time, e.g. creating
perfect hash functions and lookup table, but it doesn't seem to be
possible to achieve what you suggest.

Please file an enhancement request in bugzilla
(https://issues.dlang.org/) so we don't loose this idea.

Keep in mind though that this is mostly syntax sugar for the code above,
still requires some specing, has a somewhat uncommon use case, and
competes for our limited development time.
So don't expect anyone to jump on this, we have a lot of much higher
priority stuff to deal with.

It would become a lot more useful if you'd find out how to (perfectly?)
hash all the case labels to indices of a compact jump table.
How are tags of case classes implemented in scala?


More information about the Digitalmars-d mailing list