# Strategy for implementing Multiple Dispatch

This is a question regarding how Multiple Dispatch works.

Suppose that we have a type hierarchy like this:

``````Drawable -> Shape -> Polygon -> Rectangle
``````

And there are three functions (This is a pseudo-code):

``````void func(Rectangle*, Drawable*, Drawable*) ...
void func(Rectangle*, Rectangle*, Drawable*) ...
void func(Polygon*, Rectangle*, Rectangle*) ...
``````

And suppose that this is a piece of code:

``````Drawable* x = create_rectangle();
Drawable* y = create_rectangle();
Drawable* z = create_rectangle();
``````

Obviously types of `x,y,z` are all `Drawable` but they contain instances of a `Rectangle`. Which function will be called if I write: `func(x, y, z)`?

We don't have any function which matches exact types of `x, y and z` so what happens in this case?

You can implement this several ways. The way that would be least surprising to me is to prefer the least abstract match and to look for it from left to right.

In this case that means you'd call:

``````void func(Rectangle*, Rectangle*, Drawable*) ...
``````
• What if there are multiple such candidates? e.g. `func(Rect, Rect, Drawable)` and `func(Drawable, Rect, Rect)`? – mahdix May 17 '17 at 15:07
• Then it's an ambiguity error. – Jörg W Mittag May 17 '17 at 15:25
• Rect is less abstract then Drawable so f(RRD) wins over f(DRR) because you check this from left to right. It's just like sorting one column before another or simple alphabetizing. – candied_orange May 17 '17 at 15:33
• Be aware, the simple left to right scheme works well right up until you decide you want to allow named parameters, because suddenly calls don't have positional information you can trust. – candied_orange May 17 '17 at 19:33
• Well, you could still use arbitrary disambiguation rules, even with named paramaters: left-to-right declaration order in the parameter list, left-to-right passing order in the argument list, lexicographic ordering of the names, etc. The question is: should you? Is an arbitrary disambiguation rule better than a compile error? (My opinion is: no.) – Jörg W Mittag May 18 '17 at 15:45

I can't speak for other languages, but given your posted code,

``````func(x, y, z);
``````

is an error in C++.

If you decide to implement dynamic dispatch based on multiple arguments, you'll have to build the algorithm yourself. If you do that, one sensible strategy would be to use the best matching function based on the type of the object the arguments point to, from left to right.

If you are able to implement such a mechanism,

``````func(x, y, z);
``````

should ultimately call:

``````void func(Rectangle*, Rectangle*, Drawable*);
``````
• I know. That's why I said it's a pseudo code for a multiple dispatch code scenario – mahdix May 17 '17 at 15:07
• So `func(Rectangle*, Drawable*, Drawable*)` will have a higher priority than `func(Drawable*, Rectangle*, Rectangle*)`? – mahdix May 17 '17 at 15:35
• @mahdix, correct. – R Sahu May 17 '17 at 15:35