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
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
Types of Dispatch
Static dispatch.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
Types of Dispatch
Static dispatch.
Single dispatch.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
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
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
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
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
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
Advantages
More natural interaction with overloading.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 5 / 8
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
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
Disadvantages
May be slightly confusing.
Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 6 / 8
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
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
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
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
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

Multiple Dispatch

  • 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 Staticdispatch. Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
  • 5.
    Types of Dispatch Staticdispatch. Single dispatch. Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 3 / 8
  • 6.
    Types of Dispatch Staticdispatch. 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 Staticdispatch. 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 lookupis 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 lookupis 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 lookupis 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 interactionwith overloading. Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 5 / 8
  • 12.
    Advantages More natural interactionwith 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 interactionwith 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 slightlyconfusing. Michal P´ıˇse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 6 / 8
  • 15.
    Disadvantages May be slightlyconfusing. 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 slightlyconfusing. 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 onlyworks 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 onlyworks 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. andAldrich, 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