Why does D not have generics?

Bruce Carneal bcarneal at gmail.com
Fri Jan 15 18:11:04 UTC 2021


On Friday, 15 January 2021 at 17:36:40 UTC, Ola Fosheim Grøstad 
wrote:
> On Friday, 15 January 2021 at 17:25:59 UTC, Paulo Pinto wrote:
>> If it is instantiated T = void* then it would only be safe to 
>> use with pointers anyway, thus using T = object would be an 
>> option.
>
> That's a minor detail though.
>
> Overall, as long at D is based on GC I guess type erasure 
> should be done by the compiler for traceable pointers. Maybe 
> I'll try again for pointer-free libraries. Basically treating 
> sequences of bytes as values.
>
> For instance,  you don't really need to compare keys as double, 
> int or chars. You might as well compare them as 4 byte, 8 byte 
> etc sequences.
>
> So for a B+tree you could use the same implementation for all 
> kinds of key types.

Type erasure can be tricky, even when it is restricted to basic 
value types of the same size.  This shows up when implementing 
radix sort where one solution is to map to/from whole numbers 
(NaN semantics being ignored).

That said, good luck on your explorations.  Meta programming 
bloat is vulnerable and deserves to be taken down a peg or three.



More information about the Digitalmars-d mailing list