26
Multiple Dispatching Alex Tritthart WS 12/13

Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Multiple Dispatching

Alex Tritthart

WS 12/13

Page 2: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Outline

1 Introduction

2 Dynamic DispatchSingle DispatchDouble Dispatch

3 Multiple DispatchExample

4 Evaluation

2 / 24

Page 3: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Introduction

What is it all about?

Assume

c l a s s Animal { shout ( ) = 0}c l a s s L ion : Animal { shout ( ) {” r o a r ”}}

vo id shoutMethod ( Animal &a ){ a . shout ( ) }

Call

L ion l i o n ( ) ;shoutMethod ( l i o n ) ; // shou ld output ” r o a r ”

3 / 24

Page 4: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Introduction

What do we need?

We need

no compiling time resolution, but runtime function resolution

no function overloading, but function overriding

This is called:

Dynamic Dispatch

4 / 24

Page 5: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Introduction

What do we need?

We need

no compiling time resolution, but runtime function resolution

no function overloading, but function overriding

This is called:

Dynamic Dispatch

4 / 24

Page 6: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch

Dynamic Dispatch

Decision at runtime

Allows instance behaviour

Implemented through virtual tables

Dynamic Dispatch is every Dispatch with:

More than one possibility

Same method signature in super class and subclasses

Choosing method depending on the real types of arguments

There exist single dispatch, double dispatch, triple dispatch, ...

5 / 24

Page 7: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Single Dispatch

Single Dispatch

Definition

”The Object-Oriented term for overriding, allowing different methods tobe used depending on the type of a single object.”

6 / 24

Page 8: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Single Dispatch

Single Dispatch

Dynamic dispatch with a single type

Used mostly in object oriented languages

Simple to implement

Languages

Java, C#, JS, Python, Ruby can handle single dispatch

7 / 24

Page 9: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Single Dispatch

But how to realize?

Virtual Methods

Virtual Tables

8 / 24

Page 10: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Single Dispatch

But how to realize?

Virtual Methods

Virtual Tables

8 / 24

Page 11: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Single Dispatch

Virtual Methods

Methods which behaviour can get overridden

Methods where the vector address gets determined at runtime

Requires: same class hierarchy and same signature

Example

c l a s s Animal : p u b l i c Animal{ v i r t u a l vo id shout ( ) = 0 ; }

Languages

In C++, Object Pascal an explicit declaration is necessary

In Java, Smalltalk, Python all methods are virtual

9 / 24

Page 12: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Single Dispatch

Virtual Function Table

Each function gets an index

Each object gets a hidden pointer to the table

The compiler only generates the routing - so calls can be dissolved

Alternatives

Binary tree dispatch

String in a hash table

10 / 24

Page 13: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Single Dispatch

Virtual Function Table

11 / 24

Page 14: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Double Dispatch

Double Dispatch

Considers behavior between two objects with subclasses

Examples

Adaptive collision

Event handling systems

Printing figures on different printers

Not straight supported by OO languages like java and C++

12 / 24

Page 15: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Double Dispatch

Print all figures on all printers

vo id p r i n tA l l E v e r ywh e r e ( F i gu r e [ ] f i g u r e s , P r i n t e r [ ] p r i n t e r s )f o r ( i : f i g u r e s ) {F i gu r e f i g u r e = f i g u r e s [ i ] ;f o r ( j : p r i n t e r s ) {P r i n t e r p r i n t e r = p r i n t e r s [ j ] ;f i g u r e . p r in tOn ( p r i n t e r ) ;}}

must work for any printer or figure !

13 / 24

Page 16: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Double Dispatch

The Problem with Double Dispatch

Assume

F i gu r e f i g u r e ; // d e f a u l t : c i r c l e −f i g u r eSqua r eF i gu r e s q u a r eF i g u r e ;P r i n t e r p r i n t e r ; // d e f a u l t : b lack−white−p r i n t e rCo l o r P r i n t e r c o l o r P r i n t e r ;

Call

s q u a r eF i g u r e . p r in tOn ( p r i n t e r ) ; // squa r e i n b l a c k and wh i t es q u a r eF i g u r e . p r in tOn ( c o l o r P r i n t e r ) ; // squa r e i n c o l o r

14 / 24

Page 17: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Double Dispatch

The Problem with Double Dispatch

Assume

F i gu r e& f i g u r e R e f e r e n c e = squa r eF i g u r e ;

Call

f i g u r e R e f e r e n c e . p r in tOn ( p r i n t e r ) ; // squa r e i n b l a c k and wh i t ef i g u r e R e f e r e n c e . p r in tOn ( c o l o r P r i n t e r ) ; // squa r e i n c o l o r

15 / 24

Page 18: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Dynamic Dispatch Double Dispatch

The Problem with Double Dispatch

Assume

F i gu r e& f i g u r e R e f e r e n c e = squa r eF i g u r e ;P r i n t e r& p r i n t e r R e f e r e n c e = c o l o r P r i n t e r ;

Call

// note the type o f the p o i n t e r and the type o f the o b j e c t .f i g u r e . p r in tOn ( p r i n t e r R e f e r e n c e ) ; // c i r c l e i n c o l o r ?f i g u r e R e f e r e n c e . p r in tOn ( p r i n t e r R e f e r e n c e ) ; // squa r e i n c o l o r ?

16 / 24

Page 19: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Multiple Dispatch

Multiple Dispatch

Uses runtime types of more than one argument

Generic function chooses the appropriate method

The choice depends on the signature

Multiple Dispatch is a kind of nested instance-of checks

Languages

Common Lisp, Haskell, Dylan, Nice, Perl 6, C# 4.0

With extensions: Python, Perl, Java, C#, .NET

17 / 24

Page 20: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Multiple Dispatch

Multiple Dispatch Pattern

18 / 24

Page 21: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Multiple Dispatch Example

Example in C# 4.0:

vo id pr i n tOn Imp l ( F i gu r e x , P r i n t e r y ) {// p r i n t c i r c l e i n b l a c k and wh i t e

}vo id pr i n tOn Imp l ( Squa r eF i gu r e x , P r i n t e r y ){

// p r i n t squa r e i n b l a c k and wh i t e}. . .

vo id pr in tOn ( TopClass x , TopClass y ) {dynamic a = x ;dynamic b = y ;p r i n tOn Imp l ( a , b ) ;

}

19 / 24

Page 22: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Evaluation

Advantages

Less code to write

no argument preparation

Avoiding asymmetry of single dispatch

Generic function is relevant for several classes

Extending a class becomes nearly trivial

Languages with multiple dispatch use efficient algorithms to resolvecalls

20 / 24

Page 23: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Evaluation

Disadvantages

Single dispatch occuring most, already useable by most objectoriented languages

Median overhead of 13.7% to the maximum of 47% (study 1996)[4]

There are not that many real cases where double or triple dispatch isused

Different generic functions for one class

But: Development tools help finding all relevant functions

21 / 24

Page 24: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Evaluation

Thank you!

When should I use multiple dispatch?

Whenever you are implementing equal methods which work with a numberof different objects

Question:

Is there a workaround to get double dispatch in languages with using singledispatch?

22 / 24

Page 25: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Evaluation

Workaround: VisitorPattern

Add method to printers:

v i r t u a l vo id pr i n tOn Imp l ( F i gu r e& f i g u r e )f i g u r e . p r in tOn (∗ t h i s ) ;

Call

P r i n t e r& p r i n t e r R e f e r e n c e = c o l o r P r i n t e r ;F i gu r e& f i g u r e R e f e r e n c e = squa r eF i g u r e ;p r i n t e r R e f e r e n c e . p r in tOn ( f i g u r e ) ;p r i n t e r R e f e r e n c e . p r in tOn ( f i g u r e R e f e r e n c e ) ;

23 / 24

Page 26: Multiple Dispatchinghpac.cs.umu.se/teaching/sem-lsc-12/MultipleDispatching.pdf · 2012. 12. 12. · Dynamic Dispatch Dynamic Dispatch Decision at runtime Allows instance behaviour

Evaluation

Quellenangabe

http://c2.com/cgi/wiki?MultipleDispatch

http://c2.com/cgi/wiki?DoubleDispatchExample

Radu Muschevici, Alex Potanin, Ewan Tempero, James Noble:Multiple Dispatch in Practice http://homepages.mcs.vuw.ac.nz/

~alex/files/MuscheviciPotaninTemperoNobleOOPSLA2008.pdf

The multiple dispatch pattern in its detail

Karel Driesen, Urs Holzle: The Direct Cost of Virtual Function Calls inC++ http://homepages.mcs.vuw.ac.nz/~alex/files/

MuscheviciPotaninTemperoNobleOOPSLA2008.pdf

http:

//java.dzone.com/articles/multiple-dispatch-fix-some

24 / 24