Re: Multimethods idioms and library support
On Feb 22, 1:09 pm, itaj sherman <itajsher...@gmail.com> wrote:
Is there any concensus idiom on how to code with multimethods?
How would you go about coding them today?
There's the multi-dispatch idiom: For each override type of the first
parameter. declare a different virtual function to dispatch the second
But it requires that the definition of every polymorphic class of one
parameter #includes the definition of all polymorphic types of the
following parameter. This makes the code messy, and not scalable due
to circular dependencies between modules.
The classical solution is:
virtual void doSomething(Derived1&);
virtual void doSomething(Derived2&);
virtual void doSomething(Base& other);
class Derived1 : public Base
virtual void doSomething(Derived1& other)
// Derived1, Derived1 handling...
virtual void doSomething(Derived2& other)
// Derived1, Derived2 handling...
virtual void doSomething(Base& other)
If the hierarchy is closed (all of the derived classes
identifiable, and managed by a single entity), and not too big,
this is fine. If you want to be able to easily expand the
hierarchy, then it is far from ideal, and anything over four or
five derived classes quickly becomes overwhelming.
Multiple dispatch on open or large hierarchies is problematic in
general. For n derived classes, you need n^2 functions
(regardless of how the dispatch works), and when you add
a derived class, you have to define its interactions with all of
the existing classes. It's best avoided, but it is possible to
make it work: you need something like a
std::map<std::pair<std::type_index, std::type_index>,void (*)
, where void Base::doSomething(Base& other) isn't virtual, but
looks up the actual (free) function to call in the map. Getting
the map initialized with all of the necessary functions is
non-trivial, but if you can provide a default Base&, Base&
functionality, and only need a few specific overrides, it might
I had to use them for a some medium size hirarchy and this
idiom became quite a bother. Not to mention that users coult
not add their types becuase this idiom is circular dependant.
I'm more interested in some library that would support this in
a more generic way. I suppose any such library would need
some compiler specific code for each compiler that it
supports, but the API should be the same.
If you want an open hierarchy which supports all possible
combinations, the problem isn't the compiler; it's how to
provide all of the necessary functions. Each new class has to
provide all of the functions for it and all of the existing
classes. This quickly gets out of hand.