How to do Generic Programming in D?

bearophile bearophileHUGS at lycos.com
Tue Feb 22 13:41:08 PST 2011


Nick:

> Coming from Andrei's work in C++ "Modern C++ Programming" I wonder how 
> to implement many of those patterns in D?

Those code patterns come from many years of C++ practice from lot of C++ programmers. D2 is not so old and so used, so probably some of the idioms of D2 are yet to be invented :-)

 
> In C++ I would work with type lists and use lots of multiple inheritance 
> and templates to get the magic I need.

In D there are templates and TypeTuples, etc.


> D lacks MI, classes and delegates seem heavy (allocated on heap, with 
> one extra pointer and new for each delegate) and has instead template 
> mixins which do not create types and string mixing which create... anything.

D also has not-closure delegates (using scope, sometimes it works), function pointers, etc.


> So I am a bit lost.

D shares some things with C++, but you need to learn to use new idioms. When you go from language X to language Y there are often ways to implement the same algorithm Y too, but you need Y-specific idioms to do it. I suggest you to read the online D documentation and then Phobos code.


> Of course, the basic question being: is D2 sufficient to support 
> everything that is possible in C++?

Who knows. The answer is probably positive, but you need some creativity.

Bye,
bearophile


More information about the Digitalmars-d mailing list