Java-like generics in D

Radu radu.racariu at void.space
Sun Aug 12 14:29:19 PDT 2007


BCS wrote:
> Reply to Frank,
>
>> 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.
>>
>
> I think I have wanted something along this line:
>
> I would like to be able to have template that end up as identical be 
> just aliases to each other.
>
> for instance, with the template:
>
> |template Type(T)
> |{
> |  T Read(Stream s)
> |  {
> |    T ret;
> |    s.ReadExact(&ret, T.sizeof);
> |    return ret;
> |  }
> |}
>
> Type!(dchar).Read and Type!(int).Read are exactly the same code (read 
> and return 32bits)
> the same can be said for templates that work on classes; when two 
> types are used that derive from the same base and the base has all of 
> the methods that the template uses, then the codes is exactly the same.
.Net does something like this as it shares specializes code for a given 
class across the binary JIT image, this could be done in D at the linker 
level as an optimization pass. Could be an interesting feature.
>
> It would be nice to have some way to tell a template what parts of a 
> type to specialize on (size, how to call a given set of methods, etc.) 
> and then only when these are different will new code be generated)
>
> This would work vary well with something like a generic container for 
> objects; while instances of the container take and return a given type 
> (as the programer sees it), in fact the code is the same for all cases 
> and it only deals with general objects.
>



More information about the Digitalmars-d mailing list