How Different Are Templates from Generics

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Oct 13 04:18:38 UTC 2019


On Saturday, October 12, 2019 9:48:02 PM MDT jmh530 via Digitalmars-d-learn 
wrote:
> On Saturday, 12 October 2019 at 21:44:57 UTC, Jonathan M Davis
>
> wrote:
> > [snip]
>
> Thanks for the reply.
>
> As with most people, I don't write a lot of D code that uses
> classes that much.
>
> The use case I'm thinking of is with allocators, which - to be
> honest - is not something I deal with much in my own code.
> Basically, some of the examples have stuff like
> ScopedAllocator!Mallocator, which would imply that there is a
> different ScopedAllocator for each allocator. However, if you
> apply Java's generics, then you would just have one. Not sure if
> it would make any kind of difference in real-life code, but still
> interesting to think about.

I wouldn't think that there would be enough different allocator types to
matter much. Certainly, the amount of code that gets generated by templates
for dealing with stuff like ranges would dwarf it. If program size really
becomes a problem, then examining how code uses templates and trying to
reduce how much they're used could certainly have an impact, but I'd expect
it to be fairly rare that attempting to emulate Java's generics would help
much - especially since it would only work when classes were involved. The
main place that such an approach would have much chance of having an impact
would be with regards to container implementations when the code puts a lot
of different types of class objects inside of containers, and even that
would easily be dwarfed by all of the other template usage in your typical D
program. For Java's approach to make much sense, you'd probably have to be
writing very Java-like code.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list