SlideShare une entreprise Scribd logo
1  sur  10
Stéphane Ducasse 1
Stéphane Ducasse
stephane.ducasse@inria.fr
http://stephane.ducasse.free.fr/
Double Dispatch?
S.Ducasse 2
How to invoke a method
Depending on **both**
the receiver and an argument…
Type check are evil
Overloading is plain bad
Remember Java lookup?
Use Double Dispatch
S.Ducasse 3
Type Checking for Dispatching
How to invoke a method depending on the
receiver and an argument?
A not so good solution:
PSPrinter>>print: aDocument
^ aDocument isPS
ifTrue: [self printFromPS: aDocument]
ifFalse: [self printFromPS: aDocument asPS]
PSPrinter>>printFormPS: aPSDoc
<primitive>
PdfPrinter>>print: aDocument
^ aDocument isPS
ifTrue: [self printFromPDF: aDocument asPDF]
ifFalse: [self printFromPDF: aDocument]
PdfPrinter>>printFormPS: aPdfDoc
<primitive>
3
S.Ducasse 4
Drawbacks ofTypecheck
• Adding new kinds of documents requires
changes everywhere
• Adding new documents requires changes
everywhere
• No dynamic (without recompilation) possibilities
S.Ducasse 5
Double Dispatch
Solution: use the information given by the single
dispatch and redispatch with the argument (send a
message back to the argument passing the receiver
as an argument)
S.Ducasse 6
Double Dispatch
(a) PSPrinter>>print: aDoc
aDoc printOnPSPrinter: self
(b) PdfPrinter>>print: aDoc
aDoc printOnPdfPrinter: self
(c) PSDoc>>printOnPSPrinter: aPSPrinter
<primitive>
(d) PdfDoc>>printOnPdfPrinter: aPSPrinter
aPSprinter print: self asPS
(e) PSDoc>>printOnPSPrinter: aPdfPrinter
aPdfPrinter print: self asPdf
(f) PdfDoc>>printOnPdfPrinter:aPdfPrinter
<primitive>
SomeTests:
psprinter print: psdoc =>(a->c)
pdfprinter print: pdfdoc => (b->f)
psprinter print: pdfdoc => (a->d->b->f)
pdfprinter print: psdoc => (b->e->b->f)
S.Ducasse 7
Let’s Step Back
Example: Coercion between Float and Integer
Not a really good solution:
Integer>>+ aNumber
(aNumber isKindOf: Float)
ifTrue: [ aNumber asFloat + self]
ifFalse: [ self intAddPrimitive: aNumber]
Float>>+ aNumber
(aNumber isKindOf: Integer)
ifTrue: [aNumber asFloat + self]
ifFalse: [self floatAddPrimitive: aNumber]
Here receiver and argument are the same, we can
S.Ducasse 8
Double Dispatch on Numbers
(a) Integer>>+ aNumber
^ aNumber sumFromInteger: self
(b) Float>>+ aNumber
^ aNumber sumFromFloat: self
(c) Integer>>sumFromInteger: anInteger
<primitive: 40>
(d) Float>>sumFromInteger: anInteger
^ anInteger asFloat + self
(e) Integer>>sumFromFloat: aFloat
^aFloat + self asFloat
(f) Float>>sumFromFloat: aFloat
<primitive: 41>
Some Tests:
1 + 1: (a->c) 1 + 2.0
1.0 + 1.0: (b->f)
1 + 1.0: (a->d->b->f)
1.0 + 1: (b->e->b->f
S.Ducasse 9
Double Dispatching
• Three Kinds of Messages
–
Primary operations
–
Double dispatching methods
–
Forwarding operations
S.Ducasse 9
Double Dispatching
• Three Kinds of Messages
–
Primary operations
–
Double dispatching methods
–
Forwarding operations

Contenu connexe

En vedette (20)

Stoop 400 o-metaclassonly
Stoop 400 o-metaclassonlyStoop 400 o-metaclassonly
Stoop 400 o-metaclassonly
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
14 - Exceptions
14 - Exceptions14 - Exceptions
14 - Exceptions
 
10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)
 
Stoop 436-strategy
Stoop 436-strategyStoop 436-strategy
Stoop 436-strategy
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
15 - Streams
15 - Streams15 - Streams
15 - Streams
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop 431-visitor
Stoop 431-visitorStoop 431-visitor
Stoop 431-visitor
 

Similaire à Double Dispatch

101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirects101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirects
Acácio Oliveira
 
Algorithms and flowcharts ppt (seminar presentation)..
 Algorithms and flowcharts  ppt (seminar presentation).. Algorithms and flowcharts  ppt (seminar presentation)..
Algorithms and flowcharts ppt (seminar presentation)..
Nagendra N
 
How Danga::Socket handles asynchronous processing and how to write asynchrono...
How Danga::Socket handles asynchronous processing and how to write asynchrono...How Danga::Socket handles asynchronous processing and how to write asynchrono...
How Danga::Socket handles asynchronous processing and how to write asynchrono...
Gosuke Miyashita
 

Similaire à Double Dispatch (20)

Modules and Scripts- Python Assignment Help
Modules and Scripts- Python Assignment HelpModules and Scripts- Python Assignment Help
Modules and Scripts- Python Assignment Help
 
101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirects101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirects
 
GDB: A Lot More Than You Knew
GDB: A Lot More Than You KnewGDB: A Lot More Than You Knew
GDB: A Lot More Than You Knew
 
Algorithms and flowcharts ppt (seminar presentation)..
 Algorithms and flowcharts  ppt (seminar presentation).. Algorithms and flowcharts  ppt (seminar presentation)..
Algorithms and flowcharts ppt (seminar presentation)..
 
Embedding Pig in scripting languages
Embedding Pig in scripting languagesEmbedding Pig in scripting languages
Embedding Pig in scripting languages
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
StatsD DevOps Boulder 7/20/15
StatsD DevOps Boulder 7/20/15StatsD DevOps Boulder 7/20/15
StatsD DevOps Boulder 7/20/15
 
Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406
 
Devel::hdb debugger talk
Devel::hdb debugger talkDevel::hdb debugger talk
Devel::hdb debugger talk
 
opentsdb in a real enviroment
opentsdb in a real enviromentopentsdb in a real enviroment
opentsdb in a real enviroment
 
C# to python
C# to pythonC# to python
C# to python
 
Paexec — distributes tasks over network or CPUs
Paexec — distributes tasks over network or CPUsPaexec — distributes tasks over network or CPUs
Paexec — distributes tasks over network or CPUs
 
Lecture_6-Loops.pptx
Lecture_6-Loops.pptxLecture_6-Loops.pptx
Lecture_6-Loops.pptx
 
Python programing
Python programingPython programing
Python programing
 
How Danga::Socket handles asynchronous processing and how to write asynchrono...
How Danga::Socket handles asynchronous processing and how to write asynchrono...How Danga::Socket handles asynchronous processing and how to write asynchrono...
How Danga::Socket handles asynchronous processing and how to write asynchrono...
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Miscelaneous Debris
Miscelaneous DebrisMiscelaneous Debris
Miscelaneous Debris
 
Introduction to Command Line & Batch files
Introduction to Command Line& Batch filesIntroduction to Command Line& Batch files
Introduction to Command Line & Batch files
 
Nagios Conference 2014 - Rob Seiwert - Graphing and Trend Prediction in Nagios
Nagios Conference 2014 - Rob Seiwert - Graphing and Trend Prediction in NagiosNagios Conference 2014 - Rob Seiwert - Graphing and Trend Prediction in Nagios
Nagios Conference 2014 - Rob Seiwert - Graphing and Trend Prediction in Nagios
 
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...
DevSecCon London 2017 - MacOS security, hardening and forensics 101 by Ben Hu...
 

Plus de The World of Smalltalk (20)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
10 reflection
10 reflection10 reflection
10 reflection
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 

Dernier

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Double Dispatch

  • 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Double Dispatch?
  • 2. S.Ducasse 2 How to invoke a method Depending on **both** the receiver and an argument… Type check are evil Overloading is plain bad Remember Java lookup? Use Double Dispatch
  • 3. S.Ducasse 3 Type Checking for Dispatching How to invoke a method depending on the receiver and an argument? A not so good solution: PSPrinter>>print: aDocument ^ aDocument isPS ifTrue: [self printFromPS: aDocument] ifFalse: [self printFromPS: aDocument asPS] PSPrinter>>printFormPS: aPSDoc <primitive> PdfPrinter>>print: aDocument ^ aDocument isPS ifTrue: [self printFromPDF: aDocument asPDF] ifFalse: [self printFromPDF: aDocument] PdfPrinter>>printFormPS: aPdfDoc <primitive> 3
  • 4. S.Ducasse 4 Drawbacks ofTypecheck • Adding new kinds of documents requires changes everywhere • Adding new documents requires changes everywhere • No dynamic (without recompilation) possibilities
  • 5. S.Ducasse 5 Double Dispatch Solution: use the information given by the single dispatch and redispatch with the argument (send a message back to the argument passing the receiver as an argument)
  • 6. S.Ducasse 6 Double Dispatch (a) PSPrinter>>print: aDoc aDoc printOnPSPrinter: self (b) PdfPrinter>>print: aDoc aDoc printOnPdfPrinter: self (c) PSDoc>>printOnPSPrinter: aPSPrinter <primitive> (d) PdfDoc>>printOnPdfPrinter: aPSPrinter aPSprinter print: self asPS (e) PSDoc>>printOnPSPrinter: aPdfPrinter aPdfPrinter print: self asPdf (f) PdfDoc>>printOnPdfPrinter:aPdfPrinter <primitive> SomeTests: psprinter print: psdoc =>(a->c) pdfprinter print: pdfdoc => (b->f) psprinter print: pdfdoc => (a->d->b->f) pdfprinter print: psdoc => (b->e->b->f)
  • 7. S.Ducasse 7 Let’s Step Back Example: Coercion between Float and Integer Not a really good solution: Integer>>+ aNumber (aNumber isKindOf: Float) ifTrue: [ aNumber asFloat + self] ifFalse: [ self intAddPrimitive: aNumber] Float>>+ aNumber (aNumber isKindOf: Integer) ifTrue: [aNumber asFloat + self] ifFalse: [self floatAddPrimitive: aNumber] Here receiver and argument are the same, we can
  • 8. S.Ducasse 8 Double Dispatch on Numbers (a) Integer>>+ aNumber ^ aNumber sumFromInteger: self (b) Float>>+ aNumber ^ aNumber sumFromFloat: self (c) Integer>>sumFromInteger: anInteger <primitive: 40> (d) Float>>sumFromInteger: anInteger ^ anInteger asFloat + self (e) Integer>>sumFromFloat: aFloat ^aFloat + self asFloat (f) Float>>sumFromFloat: aFloat <primitive: 41> Some Tests: 1 + 1: (a->c) 1 + 2.0 1.0 + 1.0: (b->f) 1 + 1.0: (a->d->b->f) 1.0 + 1: (b->e->b->f
  • 9. S.Ducasse 9 Double Dispatching • Three Kinds of Messages – Primary operations – Double dispatching methods – Forwarding operations
  • 10. S.Ducasse 9 Double Dispatching • Three Kinds of Messages – Primary operations – Double dispatching methods – Forwarding operations