SlideShare a Scribd company logo
2
Most read
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

More Related Content

PPSX
Business Quiz In Iibr Pune
PDF
Tabel baja-wf-lrfd
DOCX
Perencanaan gording Baja
PDF
Laporan Struktur Rumah Tinggal
PPTX
Perencanaan gording dan penggantung
PDF
Bowplank
PPTX
Seek under Porus, the KQA History Quiz 2013 edition. Finals
Business Quiz In Iibr Pune
Tabel baja-wf-lrfd
Perencanaan gording Baja
Laporan Struktur Rumah Tinggal
Perencanaan gording dan penggantung
Bowplank
Seek under Porus, the KQA History Quiz 2013 edition. Finals

Viewers also liked (20)

PPT
PPT
PPT
Stoop 400 o-metaclassonly
PPT
Stoop 390-instruction stream
PPT
Stoop ed-class forreuse
PPT
Stoop ed-frameworks
PPT
Stoop 301-internal objectstructureinvw
PPT
Stoop ed-inheritance composition
PPT
14 - Exceptions
PPT
10 - OOP - Inheritance (a)
PPT
Stoop 436-strategy
PPT
7 - OOP - OO Concepts
PDF
Stoop 400-metaclass only
PPT
Stoop 421-design heuristics
PPT
9 - OOP - Smalltalk Classes (b)
PPT
PPT
Stoop 413-abstract classes
PPT
PPT
Stoop 305-reflective programming5
PPT
Stoop 450-s unit
Stoop 400 o-metaclassonly
Stoop 390-instruction stream
Stoop ed-class forreuse
Stoop ed-frameworks
Stoop 301-internal objectstructureinvw
Stoop ed-inheritance composition
14 - Exceptions
10 - OOP - Inheritance (a)
Stoop 436-strategy
7 - OOP - OO Concepts
Stoop 400-metaclass only
Stoop 421-design heuristics
9 - OOP - Smalltalk Classes (b)
Stoop 413-abstract classes
Stoop 305-reflective programming5
Stoop 450-s unit
Ad

Similar to Double Dispatch (20)

PDF
Live Updating Swift Code
PDF
Oop08 6
PPTX
7. Pointers and Virtual functions final -3.pptx
PPT
multiple dispatch(OOPs concepts)
PPT
Stoop 414-smalltalk elementsofdesign
PDF
Gunosy.go#7 reflect
PDF
Tour of language landscape (BuildStuff)
PDF
Swift2 smalltalk osxdev
PDF
Prototype-based Programming with JavaScript
PDF
Intro to io
PPTX
Introduction to Java -unit-1
PDF
A JIT Smalltalk VM written in itself
PDF
Ruby Programming Assignment Help
PDF
Ruby Programming Assignment Help
KEY
Runtime
PPTX
object oriented programming(PYTHON)
PPT
core java
PDF
Generics and Inference
PDF
Lecture 4: Data Types
Live Updating Swift Code
Oop08 6
7. Pointers and Virtual functions final -3.pptx
multiple dispatch(OOPs concepts)
Stoop 414-smalltalk elementsofdesign
Gunosy.go#7 reflect
Tour of language landscape (BuildStuff)
Swift2 smalltalk osxdev
Prototype-based Programming with JavaScript
Intro to io
Introduction to Java -unit-1
A JIT Smalltalk VM written in itself
Ruby Programming Assignment Help
Ruby Programming Assignment Help
Runtime
object oriented programming(PYTHON)
core java
Generics and Inference
Lecture 4: Data Types
Ad

More from The World of Smalltalk (20)

PDF
05 seaside canvas
PPT
PPT
12 virtualmachine
PPT
10 reflection
PPT
09 metaclasses
PPT
08 refactoring
PPT
07 bestpractice
PPT
PPT
03 standardclasses
PPT
Stoop sed-smells
PPT
Stoop sed-sharing ornot
PPT
Stoop sed-class initialization
PPT
Stoop sed-class initialization
PPT
Stoop metaclasses
PPT
Stoop ed-unit ofreuse
PPT
Stoop ed-subtyping subclassing
05 seaside canvas
12 virtualmachine
10 reflection
09 metaclasses
08 refactoring
07 bestpractice
03 standardclasses
Stoop sed-smells
Stoop sed-sharing ornot
Stoop sed-class initialization
Stoop sed-class initialization
Stoop metaclasses
Stoop ed-unit ofreuse
Stoop ed-subtyping subclassing

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
master seminar digital applications in india
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Institutional Correction lecture only . . .
Module 4: Burden of Disease Tutorial Slides S2 2025
STATICS OF THE RIGID BODIES Hibbelers.pdf
master seminar digital applications in india
human mycosis Human fungal infections are called human mycosis..pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Structure & Organelles in detailed.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
A systematic review of self-coping strategies used by university students to ...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Classroom Observation Tools for Teachers
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
2.FourierTransform-ShortQuestionswithAnswers.pdf

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