multiple dispatch

19

Upload: michal-pise

Post on 22-Nov-2014

750 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Multiple Dispatch
Page 2: Multiple Dispatch

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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