digitalmars.D.learn - Double-dispatch
- Sean Eskapp (8/8) Feb 13 2011 I remember in C++, I had to do double-dispatch using the visitor pattern...
- Simen kjaeraas (10/19) Feb 13 2011 Andrei Alexandrescu (yes, the same one) wrote a generic implementation o...
- Lutger Blijdestijn (19/29) Feb 13 2011 There isn't really, but you can take out some of the boilerplate with a
- bearophile (4/5) Feb 13 2011 Andrei has recently said no one needs double dispatch (in D) :-) So Andr...
- Sean Eskapp (3/8) Feb 13 2011 The age-old collision handling problem is how I'm using it.
- Peter Alexander (3/11) Feb 16 2011 Use a 2D array of function pointers, with the indices coming from unique...
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?
Feb 13 2011
Sean Eskapp <eatingstaples 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
Feb 13 2011
Sean Eskapp 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?There isn't really, but you can take out some of the boilerplate with a mixin, which goes a long way: // The template parameter Visitor is not actually needed, depending on // what you want to do mixin template Accept(Visitor) { void accept(Visitor v) { v.visit(this); } } class Stuff { mixin Accept!IStuffVisitor; } With some ctfe it's also not too hard to take out the boilerplate of creating the visitor interface with a bit of code generation, though I'm not sure if it's worth it.
Feb 13 2011
Sean Eskapp:Is there a nicer way to do this in D, or am I stuck with the same thing?Andrei has recently said no one needs double dispatch (in D) :-) So Andrei will be interested in your use case. Bye, bearophile
Feb 13 2011
== Quote from bearophile (bearophileHUGS lycos.com)'s articleSean Eskapp:be interested in your use case.Is there a nicer way to do this in D, or am I stuck with the same thing?Andrei has recently said no one needs double dispatch (in D) :-) So Andrei willBye, bearophileThe age-old collision handling problem is how I'm using it.
Feb 13 2011
On 13/02/11 9:32 PM, Sean Eskapp wrote:== Quote from bearophile (bearophileHUGS lycos.com)'s articleUse a 2D array of function pointers, with the indices coming from unique IDs on the shapes.Sean Eskapp:be interested in your use case.Is there a nicer way to do this in D, or am I stuck with the same thing?Andrei has recently said no one needs double dispatch (in D) :-) So Andrei willBye, bearophileThe age-old collision handling problem is how I'm using it.
Feb 16 2011