Double-dispatch

Simen kjaeraas simen.kjaras at gmail.com
Sun Feb 13 11:00:11 PST 2011


Sean Eskapp <eatingstaples at gmail.com> wrote:

> I remember in C++, I had to do double-dispatch using the visitor  
> pattern. This
> is cumbersome, just because each subclass has to have the exact same
> singly-dispatched code that looks like:
>
> void dispatch(Base& other)
> {
>    other.dispatch(*this);
> }
>
> Is there a nicer way to do this in D, or am I stuck with the same thing?

Andrei Alexandrescu (yes, the same one) wrote a generic implementation of
multimethods[1] for his book Modern C++ Design[2]. You might want to take
a look at that. If you decide to re-implement it in D, it might also be
worth incorporating in Phobos.

[1]:  
http://loki-lib.cvs.sourceforge.net/loki-lib/loki/include/loki/MultiMethods.h?view=markup
[2]: http://www.amazon.com/a/dp/0201704315/

-- 
Simen


More information about the Digitalmars-d-learn mailing list