2. Motivation
void f(String o) { ... }
void f(Object o) { ... }
Object o = new String("foo");
f(o);
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 2 / 8
3. Motivation
void f(String o) { ... }
void f(Object o) { ... }
Object o = new String("foo");
f(o);
Invoked function may change with the static type of o.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 2 / 8
4. Types of Dispatch
Static dispatch.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
5. Types of Dispatch
Static dispatch.
Single dispatch.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
6. Types of Dispatch
Static dispatch.
Single dispatch.
Multiple dispatch.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
7. Types of Dispatch
Static dispatch.
Single dispatch.
Multiple dispatch.
. . .
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
8. Multiple Dispatch
Method lookup is based on runtime classes of parameters.
Does not depend on static types of parameters.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 4 / 8
9. Multiple Dispatch
Method lookup is based on runtime classes of parameters.
Does not depend on static types of parameters.
Can be emulated (albeit in a restricted fashion) by the visitor pattern.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 4 / 8
10. Multiple Dispatch
Method lookup is based on runtime classes of parameters.
Does not depend on static types of parameters.
Can be emulated (albeit in a restricted fashion) by the visitor pattern.
Resembles (in a way) pattern matching.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 4 / 8
11. Advantages
More natural interaction with overloading.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 5 / 8
12. Advantages
More natural interaction with overloading.
More equal parameters.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 5 / 8
13. Advantages
More natural interaction with overloading.
More equal parameters.
Better composability.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 5 / 8
14. Disadvantages
May be slightly confusing.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 6 / 8
15. Disadvantages
May be slightly confusing.
Blurred notion of method containment.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 6 / 8
16. Disadvantages
May be slightly confusing.
Blurred notion of method containment.
The diamond problem may cause ambiguity.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 6 / 8
17. CZ
Multiple dispatch only works across inheritance hierarchy (not across
requires hierarchy).
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 7 / 8
18. CZ
Multiple dispatch only works across inheritance hierarchy (not across
requires hierarchy).
No diamonds in inheritance hierarchy.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 7 / 8
19. See
Malayeri, D. and Aldrich, J. CZ: Multiple Inheritance Without
Diamonds. Proceeding of the 24th ACM SIGPLAN Conference on Object
Oriented Programming Systems Languages and Applications. 21–40.
http://doi.acm.org/10.1145/1640089.1640092
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 8 / 8