Java-like generics in D

Radu radu.racariu at void.space
Sun Aug 12 14:01:39 PDT 2007


Frank Benoit wrote:
> Radu schrieb:
>   
>> Why on earth should some one want to implement such a hack that Java
>> generics are?
>> I think you completely miss understand how templates (generic code) work
>> and how bad the Java implementation is. On .Net at least they are more
>> than just compiler syntactic sugar. So no, it does not make any sense!
>>     
>
> Several times, i tried to use templates in D. And each time, again i
> meet these problems:
> - errors about forward reverences
> - increasing executable size by massive template instantiation with
> differnt types.
>   
First one is a bug, so no need to invent a new way to write generic code 
but rather fix the bug.
I have to see a case where size bloat really does matter.
> So my understanding is:
> * templates are perfect to parametrize with primitives, where a
> different type parameter generated different binary code. (optimized code)
> * templates are used to make compile time features possible.
>
>   
templates are perfect for any parametrized type as we need to write 
generic code that performs well and also write smart generic code as D 
templates are Turing complete.

> But if you make something like a simple container, they are not so nice.
>  A generic would be more appropriate in this case. In this case i wish i
> had that java hack available. Thats why i though generics as an addition
> to templates could be a good thing.
>   
You can certainly emulate generics by specializing your templates to 
void* or Object and I think even the casting can be done using D 
templates and meta programming functions. Java generics are implemented 
as a hack in the compiler and spec as the JVM doesn't really know 
anything about the type your container is using, everything is just 
Object with some casts, this comes from lack of support on the bytecode 
level and the need to keep backwards compatibility. This is why generics 
have so little usage across the language as you're limited by lack of 
expressiveness and power offered otherwise by true generic code.



More information about the Digitalmars-d mailing list