Questions tagged [double-dispatch]

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.

97 questions
1
vote
1 answer

Double dispatch using visitor pattern in Java

I have this architecture: In XMLFormulaFormatter, I need the value of instances of Constant (instances which are created in my Main class). I have this method in the Constant class: public double asValue() { return value ; } I have tried this code…
1
vote
1 answer

Wrong double dispatch method when putting a vctrs-built class in a package

I've created a new class to print percentages with vctrs, like explained in https://vctrs.r-lib.org/articles/s3-vector.html . It works well when I source the .R file. But when I build the package with devtools, basic operations made possible with…
1
vote
1 answer

How is an "isa?" based multimethod more than syntax sugar for instanceof?

I am taking an example from the clojure site. (defmulti foo class) (defmethod foo ::collection [c] :a-collection) (defmethod foo String [s] :a-string) (foo []) :a-collection (foo (java.util.HashMap.)) :a-collection (foo "bar") :a-string This…
Daniel
  • 546
  • 1
  • 6
  • 15
1
vote
3 answers

Implementing double dispatch with two class hierarchies in C++

I want to create an event dispatch system with a (shallow) hierarchy of Events that can be observed by a (shallow) hierarchy of EventObservers. I figured double-dispatch would allow a wide variety of both Events and EventObservers without having to…
BrettW
  • 37
  • 6
1
vote
2 answers

std::shared_ptr and double callback

I have some logic where I am using std::shared_ptrs to objects in an inheritance hierarchy. At one point I need to handle these objects depending on their real type, so I am using a double dispatch (i.e. I call a method on the base class, which then…
LiKao
  • 10,408
  • 6
  • 53
  • 91
1
vote
1 answer

Overloading method without modifying classes

I have access to a class structure, which I cannot modify, as follows: Graphics Circle Line etc. Again, I cannot modify it! These all have individual properties such as Radius, FirstPoint, LastPoint, etc., as well as some common properties. I…
Nick Bull
  • 9,518
  • 6
  • 36
  • 58
1
vote
0 answers

C++ Double dispatch with runtime polymorphism?

Is it possible to perform double dispatch with runtime polymorphism? Say I have some classes, and some of those classes can be added/multiplied/etc., and I want to store those dynamically within another class that performs type erasure at runtime.…
Adam
  • 303
  • 3
  • 11
1
vote
1 answer

C++ double dispatch example

I got this code as an example of use of double dispatch, but I don't really understand one part of the code. creating the "abstract class" Printer, why I need to add: virtual void print(PDFDoc *d)=0; virtual void print(DocDoc *d)=0; As I…
Mr.O
  • 342
  • 2
  • 19
1
vote
2 answers

How to Emulate Double Dispatch in Objective-C

I'm, trying to emulate a Double Dispatch in Objective-C. I know Objective-C does not support function/method overloading. But can you emulate this? Any idea.
Josué H.
  • 1,171
  • 3
  • 15
  • 35
1
vote
1 answer

Can the below program be considered double dispatch example?

class Product { } class Mobile extends Product { } class Market { public void buy(Product product) { System.out.println("Search a product in market"); } public void buy(Mobile mobile) { …
ahskid
  • 13
  • 4
1
vote
1 answer

Multiple dispatch and multi-methods

What are they, what's the different between them? Many sources, like Wikipedia, claim they're the same thing, but others explicitly say the opposite, like sbi in this question: First: "Visitor Pattern is a way to simulate Double Dispatching in…
peoro
  • 25,562
  • 20
  • 98
  • 150
1
vote
2 answers

Mixing double dispatch and static polymorphism

I'm sure this is a bad idea. Let's pretend I have a good reason to do it. I have a tree of nodes that successfully uses static polymorphism to pass messages. Crucially, each node cannot the types of the nodes it connects to, it just knows the types…
Ian
  • 2,078
  • 1
  • 17
  • 27
1
vote
0 answers

Is there a C++/CLI equivalent to a C# cast to a dynamic type?

I'm extending a class hierarchy written in C# that implements a Visitor pattern (double-dispatch) using the dynamic keyword, as described here. Here is some very simplified pseudo-code of what I'm currently doing in C#: public class Command { …
BlueStrat
  • 2,202
  • 17
  • 27
1
vote
1 answer

Circular dependency with double dispatch

I'm trying to implement the double dispatch pattern but I get a circular dependency that I can't resolve with forward declaration (as it was resolved in this problem link). Below is an example of my problem: header 1: class Object { virtual void…
dlavila
  • 1,204
  • 11
  • 25
1
vote
3 answers

How can I implement double dispatch when I don't know all the classes in advance?

I've got a base class with (potentially) a lot of subclasses, and I would like to be able to compare any two objects of the base class for equality. I am trying to do this without invoking the blasphemous typeid keyword. #include struct…