Why does D not have generics?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sun Jan 17 10:37:10 UTC 2021


On Sunday, 17 January 2021 at 04:03:30 UTC, Bruce Carneal wrote:
> I've been working on my current project for a few months but 
> I'm still not ready to talk about it in detail.  I will say 
> that it does not sort anything (doesn't have the power/compute 
> budget for sorting) and that it does employ the bijection as a 
> type erasure/restoration mechanism.

This "bijection" strategy is interesting.  (I assume you think of 
bijective functions in mathematics?) For some reason I've never 
really given it much thought. But it makes a lot of sense for 
encoding keys. Some online services also only accept keys that 
are either strings or 64bit integers, so it might be useful in 
other contexts.

I've written a generic key type that does this encoding (in C++, 
but easy to port to D), and are working on a compressing one. So, 
for instance if you have a tuple of (z,y,x) then you can specify 
that 1≤x≤31, 1≤y≤12, 1970≤z≤2030 and let it be compressed either 
by bit shifting or multiplication (slower but tighter). So for 
instance (2021,1,17) would compress as

((2021-1970)*12+0)*31+16

Is there some way to find out the range (min, max) of an enum?

The next step is to cover keys larger than 64 bits, I guess I 
should cover up to 256 bits.




More information about the Digitalmars-d mailing list