Java-like generics in D

Bill Baxter dnewsgroup at billbaxter.com
Sun Aug 12 12:56:55 PDT 2007


Robert Fraser wrote:
> Frank Benoit Wrote:
> 
>> As addition to the existing templates, i think java like generics
>> would be very helpful for D.
>> 
> 
> I think that's an interesting, but wholly unnecessary idea. Generics
> were added to Java because first-class templates were an
> impossibility given the bytecode structure. They're a workaround, not
> a feature to be emulated in other languages.
> 
>> Advantages: 5) "lightweight templates", because of no more object
>> code duplication for new instantiations.
> 
> Is object code duplication a huge problem? Are you running into all
> sorts of code bloat in your projects? D programs _do_ tend to wax a
> little on the large side, but code bloat is hardly the primary factor
> of this.
> 

> I get the feeling they would be generally unused and woefully
> underpowered compared to templates. Look at the uses for generics in
> Java: almost exclusively in collections and wrapper classes. 

I think that's the point.  If all you're writing is a collection class 
then maybe you don't really need the full power / bloat of D templates.

In C++ there's some sort of trick they use to avoid template bloat in 
which a specialization for void* is used to implement the template for 
all pointer types.  Then the only thing that actually gets instantiated 
multiple times is some lightweight interface code that does casting to 
and from UserType to void*.  I think that's essentially what generics do 
for you under the hood.  The same approach should work in D, I would think.

--bb



More information about the Digitalmars-d mailing list