In software engineering, double dispatch is a special form of multiple dispatch and a mechanism that dispatches a function call to different concrete functions depending on the runtime types of two objects involved in the call. In most OO systems, the concrete function that is called from a function call in the code depends on the dynamic type of a single object and therefore they are known as single dispatch calls, or simply virtual function calls.
Questions tagged [double-dispatch]
97 questions
3
votes
1 answer
Visitor Pattern and Double Dispatch
I know this is well trodden territory but I have a specific question... I promise.
Having spent very little time in the statically typed, object oriented world, I recently came across this design pattern while reading Crafting Interpreters. While I…

Solaxun
- 2,732
- 1
- 22
- 41
3
votes
4 answers
given abstract base class X, how to create another template class D where T is the type of the class deriving from X?
I want to be able to accept a Message& object which references either a Message1 or Message2 class. I want to be able to create a MessageWithData or MessageWithData based on the underlying type of the Message& object. For…

Kyle
- 4,487
- 3
- 29
- 45
3
votes
1 answer
Storing vector of std::shared_ptr where Foo is a templated class
I have a base class that I made a template because I want to vary the type it takes for several functions, but I want to derive from these templated base classes. I want to store a vector of these classes. My idea was to create a non-templated…

Matt
- 5,478
- 9
- 56
- 95
3
votes
3 answers
Double dispatch in C++ not working
I'm writing a rogue-like game in C++ and have problems with double dispatch.
class MapObject {
virtual void collide(MapObject& that) {};
virtual void collide(Player& that) {};
virtual void collide(Wall& that) {};
…

user7369463
- 165
- 1
- 8
3
votes
0 answers
double dispatch in swift for numeric types
Like many people I am trying to work with both integers and floating points and collections of these types.
However the Swift type system is not always that forgivable. Most people define a Numeric protocol to deal with this. I would like to know if…

user965972
- 2,489
- 2
- 23
- 39
3
votes
1 answer
Double Dispatch and inheritance
I have a number of dumb object classes that I would like to serialize as Strings for the purpose of out-of-process storage. This is a pretty typical place to use double-dispatch / the visitor pattern.
public interface Serializeable {
T…

Huckle
- 1,810
- 3
- 26
- 40
3
votes
2 answers
operator== with double dispatch in C++
How should one implement
operator==(const Base& base)
to compare subclasses s.t. the calls would be properly dispatched when called as
Base* base1 = new Derived1();
Base* base2 = new Derived2();
base1->operator==(*base2)?

MAG
- 85
- 8
3
votes
0 answers
Visitor pattern lacking parameters
I'm sure this must be a common problem with the Visitor pattern, so thought I'd see if there is a standard solution.
How can you re-code a tree traversal where the methods are built into the tree classes themselves, say
class Node {
void…

vcvcvcb
- 31
- 2
3
votes
1 answer
method with two parameters which both need to be double dispatched
lets say i have a method which has two parameters. i have been implementing them as:
if(aObj instance of Marble) {
if(bObj instance of Bomb) {
this.resolve((Marble)aObj,(Bomb)bObj);
}
}
as you can see its not a very pretty solution.…

mixm
- 531
- 2
- 4
- 18
3
votes
1 answer
How to build double dispatch using extensions
I have a library that have a hierarchy of class as follow:
class Base {}
class A : Base {}
class B : Base {}
Now I wanted to do different thing depending on type of my object whether it is an A or a B.
So I decide to go for and implement double…

mathk
- 7,973
- 6
- 45
- 74
3
votes
7 answers
.Net 4.0 Optimized code for refactoring existing "if" conditions and "is" operator
I have following C# code. It works fine; but the GetDestination() method is cluttered with multiple if conditions by using is operator.
In .Net 4.0 (or greater) what is the best way to avoid these “if” conditions?
EDIT: Role is part of the business…

LCJ
- 22,196
- 67
- 260
- 418
3
votes
3 answers
Java Class.cast() and Overload
I'm trying to code a packet listener for a little server. I'm very new to Java and this is the first time i mess around with networking. The whole idea it's recive the packet, match the packet id with it's class, pass the input stream to the…

Robert
- 35
- 5
3
votes
3 answers
Double dispatch for collision handling with SpriteKit
I'm using SpriteKit's collision detection. It has a callback that looks like this:
- (void)didBeginContact:(SKPhysicsContact *)contact
The contact object has two physics bodies:
SKPhysicsBody *bodyA;
SKPhysicsBody *bodyB;
My game will have lots of…

nont
- 9,322
- 7
- 62
- 82
2
votes
1 answer
c++ double dispatch observer notification
Here is the code I am currently troubleshooting:
void CTimer::notify()
{
std::vector::iterator it;
for(it=observers.begin();it!=observers.end();++it)
{
ITimerNotification* notification = new…

Brian
- 145
- 1
- 3
- 13
2
votes
0 answers
Double dispatch in R: S4 vs vctrs library
If we want to implement a double dispatch method in R, we currently have two options to choose from:
S4 methods
vctrs library S3-based double dispatch
I especially mean arithmetic operators, such as + or *.
How do they compare in terms of…

KrzJoa
- 71
- 2
- 4