multiple dispatch
DESCRIPTION
TRANSCRIPT
![Page 1: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/1.jpg)
![Page 2: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/2.jpg)
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
![Page 3: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/3.jpg)
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
![Page 4: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/4.jpg)
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
![Page 5: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/5.jpg)
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
![Page 6: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/6.jpg)
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
![Page 7: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/7.jpg)
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
![Page 8: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/8.jpg)
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
![Page 9: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/9.jpg)
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
![Page 10: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/10.jpg)
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
![Page 11: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/11.jpg)
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
![Page 12: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/12.jpg)
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
![Page 13: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/13.jpg)
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
![Page 14: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/14.jpg)
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
![Page 15: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/15.jpg)
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
![Page 16: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/16.jpg)
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
![Page 17: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/17.jpg)
CZ
Multiple dispatch only works across inheritance hierarchy (not acrossrequires hierarchy).
No diamonds in inheritance hierarchy.
Michal Pıse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 7 / 8
![Page 18: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/18.jpg)
CZ
Multiple dispatch only works across inheritance hierarchy (not acrossrequires hierarchy).
No diamonds in inheritance hierarchy.
Michal Pıse (CTU in Prague) Object Programming L. 6: Multiple Dispatch November 2, 2010 7 / 8
![Page 19: Multiple Dispatch](https://reader033.vdocuments.mx/reader033/viewer/2022051513/547243edb4af9f980a8b4df5/html5/thumbnails/19.jpg)
See
Malayeri, D. and Aldrich, J. CZ: Multiple Inheritance WithoutDiamonds. Proceeding of the 24th ACM SIGPLAN Conference on ObjectOriented 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