<s>
In	O
software	B-General_Concept
engineering	I-General_Concept
,	O
double	B-Language
dispatch	I-Language
is	O
a	O
special	O
form	O
of	O
multiple	O
dispatch	O
,	O
and	O
a	O
mechanism	O
that	O
dispatches	O
a	O
function	O
call	O
to	O
different	O
concrete	O
functions	O
depending	O
on	O
the	O
runtime	O
types	O
of	O
two	O
objects	O
involved	O
in	O
the	O
call	O
.	O
</s>
<s>
In	O
most	O
object-oriented	B-Language
systems	O
,	O
the	O
concrete	O
function	O
that	O
is	O
called	O
from	O
a	O
function	O
call	O
in	O
the	O
code	O
depends	O
on	O
the	O
dynamic	O
type	O
of	O
a	O
single	O
object	O
and	O
therefore	O
they	O
are	O
known	O
as	O
single	O
dispatch	O
calls	O
,	O
or	O
simply	O
virtual	B-Application
function	I-Application
calls	O
.	O
</s>
<s>
Dan	O
Ingalls	O
first	O
described	O
how	O
to	O
use	O
double	B-Language
dispatching	I-Language
in	O
Smalltalk	B-Application
,	O
calling	O
it	O
multiple	B-Language
polymorphism	I-Language
.	O
</s>
<s>
To	O
that	O
end	O
,	O
systems	O
like	O
CLOS	B-Application
implement	O
multiple	O
dispatch	O
.	O
</s>
<s>
Double	B-Language
dispatch	I-Language
is	O
another	O
solution	O
that	O
gradually	O
reduces	O
the	O
polymorphism	O
on	O
systems	O
that	O
do	O
not	O
support	O
multiple	O
dispatch	O
.	O
</s>
<s>
Double	B-Language
dispatch	I-Language
is	O
useful	O
in	O
situations	O
where	O
the	O
choice	O
of	O
computation	O
depends	O
on	O
the	O
runtime	O
types	O
of	O
its	O
arguments	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
programmer	O
could	O
use	O
double	B-Language
dispatch	I-Language
in	O
the	O
following	O
situations	O
:	O
</s>
<s>
In	O
C++	B-Language
,	O
for	O
example	O
,	O
a	O
dynamic	O
function	O
call	O
is	O
usually	O
resolved	O
by	O
a	O
single	O
offset	B-General_Concept
calculation	O
-	O
which	O
is	O
possible	O
because	O
the	O
compiler	O
knows	O
the	O
location	O
of	O
the	O
function	O
in	O
the	O
object	O
's	O
method	B-Language
table	I-Language
and	O
so	O
can	O
statically	O
calculate	O
the	O
offset	B-General_Concept
.	O
</s>
<s>
In	O
a	O
language	O
supporting	O
double	B-Language
dispatch	I-Language
,	O
this	O
is	O
slightly	O
more	O
costly	O
,	O
because	O
the	O
compiler	O
must	O
generate	O
code	O
to	O
calculate	O
the	O
method	O
's	O
offset	B-General_Concept
in	O
the	O
method	B-Language
table	I-Language
at	O
runtime	O
,	O
thereby	O
increasing	O
the	O
overall	O
instruction	B-General_Concept
path	I-General_Concept
length	I-General_Concept
(	O
by	O
an	O
amount	O
that	O
is	O
likely	O
to	O
be	O
no	O
more	O
than	O
the	O
total	O
number	O
of	O
calls	O
to	O
the	O
function	O
,	O
which	O
may	O
not	O
be	O
very	O
significant	O
)	O
.	O
</s>
<s>
At	O
first	O
glance	O
,	O
double	B-Language
dispatch	I-Language
appears	O
to	O
be	O
a	O
natural	O
result	O
of	O
function	B-Language
overloading	I-Language
.	O
</s>
<s>
Function	B-Language
overloading	I-Language
allows	O
the	O
function	O
called	O
to	O
depend	O
on	O
the	O
type	O
of	O
the	O
argument	O
.	O
</s>
<s>
Function	B-Language
overloading	I-Language
,	O
however	O
,	O
is	O
done	O
at	O
compile	O
time	O
using	O
"	O
name	B-Language
mangling	I-Language
"	O
where	O
the	O
internal	O
name	O
of	O
the	O
function	O
encodes	O
the	O
argument	O
's	O
type	O
.	O
</s>
<s>
Thus	O
,	O
there	O
is	O
no	O
name	O
collision	O
,	O
and	O
no	O
virtual	B-Language
table	I-Language
lookup	O
.	O
</s>
<s>
By	O
contrast	O
,	O
dynamic	O
dispatch	O
is	O
based	O
on	O
the	O
type	O
of	O
the	O
calling	O
object	O
,	O
meaning	O
it	O
uses	O
virtual	B-Application
functions	I-Application
(	O
overriding	O
)	O
instead	O
of	O
function	B-Language
overloading	I-Language
,	O
and	O
does	O
result	O
in	O
a	O
vtable	B-Language
lookup	O
.	O
</s>
<s>
Consider	O
the	O
following	O
example	O
,	O
written	O
in	O
C++	B-Language
,	O
of	O
collisions	O
in	O
a	O
game	O
:	O
</s>
<s>
then	O
,	O
because	O
of	O
function	B-Language
overloading	I-Language
,	O
</s>
<s>
The	O
problem	O
is	O
that	O
,	O
while	O
virtual	B-Application
functions	I-Application
are	O
dispatched	O
dynamically	O
in	O
C++	B-Language
,	O
function	B-Language
overloading	I-Language
is	O
done	O
statically	O
.	O
</s>
<s>
The	O
problem	O
described	O
above	O
can	O
be	O
resolved	O
by	O
simulating	O
double	B-Language
dispatch	I-Language
,	O
for	O
example	O
by	O
using	O
a	O
visitor	B-Language
pattern	I-Language
.	O
</s>
<s>
theSpaceShipReference	O
is	O
a	O
reference	O
,	O
so	O
C++	B-Language
looks	O
up	O
the	O
correct	O
method	O
in	O
the	O
vtable	B-Language
.	O
</s>
<s>
Within	O
ApolloSpacecraft::CollideWith	O
( Asteroid&	O
)	O
,	O
inAsteroid	O
is	O
a	O
reference	O
,	O
so	O
inAsteroid.CollideWith	O
( *this	O
)	O
will	O
result	O
in	O
another	O
vtable	B-Language
lookup	O
.	O
</s>
<s>
In	O
C#	B-Application
,	O
when	O
calling	O
an	O
instance	O
method	O
accepting	O
an	O
argument	O
,	O
multiple	O
dispatch	O
can	O
be	O
achieved	O
without	O
employing	O
the	O
visitor	B-Language
pattern	I-Language
.	O
</s>
<s>
The	O
run-time	O
binder	O
will	O
choose	O
the	O
appropriate	O
method	B-Language
overload	I-Language
at	O
run-time	O
.	O
</s>
<s>
The	O
Eiffel	B-Language
programming	I-Language
language	I-Language
can	O
bring	O
the	O
concept	O
of	O
agents	O
to	O
bear	O
on	O
the	O
double-dispatch	O
problem	O
.	O
</s>
<s>
We	O
want	O
objects	O
of	O
the	O
two	O
types	O
to	O
co-variantly	O
interact	O
with	O
each	O
other	O
in	O
a	O
double-dispatch	O
using	O
a	O
visitor	B-Language
pattern	I-Language
.	O
</s>
<s>
The	O
output	O
example	O
below	O
shows	O
the	O
results	O
of	O
two	O
SURFACE	O
visitor	B-Language
objects	O
being	O
polymorphically	O
passed	O
over	O
a	O
list	O
of	O
polymorphic	O
SHAPE	O
objects	O
.	O
</s>
<s>
The	O
visitor	B-Language
code	O
pattern	O
is	O
only	O
aware	O
of	O
SHAPE	O
and	O
SURFACE	O
generically	O
and	O
not	O
of	O
the	O
specific	O
type	O
of	O
either	O
.	O
</s>
<s>
The	O
visitor	B-Language
pattern	I-Language
works	O
by	O
way	O
of	O
a	O
visitor	B-Language
object	O
visiting	O
the	O
elements	O
of	O
a	O
data	O
structure	O
(	O
e.g.	O
</s>
<s>
It	O
passes	O
an	O
indirect	O
and	O
polymorphic	O
agent	O
( `drawing_data_agent	O
 '	O
)	O
,	O
allowing	O
our	O
visitor	B-Language
code	O
to	O
only	O
know	O
about	O
two	O
things	O
:	O
</s>
<s>
Because	O
both	O
SURFACE	O
and	O
SHAPE	O
define	O
their	O
own	O
agents	O
,	O
our	O
visitor	B-Language
code	O
is	O
freed	O
from	O
having	O
to	O
know	O
what	O
is	O
the	O
appropriate	O
call	O
to	O
make	O
,	O
polymorphically	O
or	O
otherwise	O
.	O
</s>
<s>
This	O
level	O
of	O
indirection	O
and	O
decoupling	O
is	O
simply	O
not	O
achievable	O
in	O
other	O
common	O
languages	O
like	O
C	O
,	O
C++	B-Language
and	O
Java	O
except	O
through	O
either	O
some	O
form	O
of	O
reflection	B-Language
or	O
feature	O
overloading	O
with	O
signature	O
matching	O
.	O
</s>
<s>
This	O
is	O
a	O
distinct	O
advantage	O
of	O
Eiffel	B-Language
agents	O
over	O
the	O
single	O
inheritance	O
,	O
dynamic	O
and	O
polymorphic	O
binding	O
of	O
other	O
languages	O
.	O
</s>
<s>
This	O
means	O
that	O
the	O
visitor	B-Language
pattern	I-Language
code	O
(	O
who	O
is	O
the	O
ONLY	O
client	O
of	O
this	O
class	O
)	O
only	O
needs	O
to	O
know	O
about	O
the	O
agent	O
to	O
get	O
its	O
job	O
done	O
(	O
e.g.	O
</s>
<s>
The	O
visitor	B-Language
for	O
the	O
classic	O
Spaceship	O
example	O
also	O
has	O
a	O
double-dispatch	O
mechanism	O
.	O
</s>
<s>
This	O
is	O
similar	O
to	O
C	O
,	O
C++	B-Language
and	O
Java	O
scopes	O
of	O
Private	O
.	O
</s>
<s>
With	O
regards	O
to	O
double-dispatch	O
,	O
Eiffel	B-Language
allows	O
the	O
designer	O
and	O
programmer	O
to	O
further	O
remove	O
a	O
level	O
of	O
direct	O
object-to-object	O
knowledge	O
by	O
decoupling	O
class	O
routines	O
from	O
their	O
classes	O
by	O
way	O
of	O
making	O
them	O
agents	O
and	O
then	O
passing	O
those	O
agents	O
instead	O
of	O
making	O
direct	O
object	O
feature	O
calls	O
.	O
</s>
<s>
With	O
this	O
in	O
mind	O
,	O
one	O
ought	O
never	O
to	O
presume	O
the	O
use	O
of	O
agents	O
in	O
the	O
double-dispatch	O
and	O
their	O
application	O
in	O
visitor	B-Language
patterns	I-Language
.	O
</s>
