Multiple Dispatch

Post on 22-Nov-2014

742 views 0 download

description

 

transcript

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

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.

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

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.

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

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

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.

More equal parameters.

Better composability.

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

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.

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

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

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 acrossrequires hierarchy).

No diamonds in inheritance 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 acrossrequires 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 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