Questions tagged [dynamic-dispatch]

In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at runtime.

In , dynamic dispatch is the process of selecting which implementation of a polymorphic operation ( or ) to at .

Dynamic dispatch contrasts with static dispatch in which the implementation of a polymorphic operation is selected at . The purpose of dynamic dispatch is to support cases where the appropriate implementation of a operation can't be determined at compile time because it depends on the runtime type of one or more actual parameters to the operation.

Dynamic dispatch is different from or . In the context of selecting an operation, refers to the process of associating a name with an operation.

Dispatching refers to choosing an implementation for the operation after you have decided which operation a name refers to.

With dynamic dispatch, the name may be bound to a polymorphic operation at compile time, but the implementation not be chosen until runtime (this is how dynamic dispatch works in C++). However, late binding does imply dynamic dispatching since you cannot choose which implementation of a polymorphic operation to select until you have selected the operation that the name refers to.

Dynamic dispatch is often used in languages when different classes contain different implementations of the same method due to common inheritance. For example, suppose you have classes A, B, and C, where B and C both inherit the method foo() from A. Now suppose x is variable of class A. At run time, x may actually have a value of type B or C and in general you can't know what it is at compile time.

With static dispatch, a x.foo() will always refer to the implementation of foo() for class A because static binding only looks at the declared type of the object. With dynamic dispatch the language will determine the type of the value of x at run-time and call the version of foo() that is associated with whatever type the value has, whether A, B, or C.

Source:http://en.wikipedia.org/wiki/Dynamic_dispatch

169 questions
53
votes
4 answers

Dynamic dispatch in Haskell

Programs written in, for example, Java rely a lot on dynamic dispatch. How are such programs expressed in functional languages such as Haskell? In other words, what is the Haskell way of expressing the idea underneath "dynamic dispatch"?
user782220
  • 10,677
  • 21
  • 72
  • 135
47
votes
3 answers

The trait cannot be made into an object

I have the following code: extern crate futures; // 0.1.24 use futures::Future; use std::io; struct Context; pub trait MyTrait { fn receive(context: Context) -> Future; } pub struct MyStruct { my_trait:…
Alexander
  • 833
  • 2
  • 8
  • 17
30
votes
4 answers

Swift protocol extension method is called instead of method implemented in subclass

I've encountered a problem that is explained in the code below (Swift 3.1): protocol MyProtocol { func methodA() func methodB() } extension MyProtocol { func methodA() { print("Default methodA") } func methodB() { …
Igor Kulagin
  • 1,701
  • 15
  • 20
27
votes
6 answers

Does Swift have dynamic dispatch and virtual methods?

Coming form a C++/Java/C# background I was expecting to see virtual methods in Swift, however reading the swift documentation I see no mention of virtual methods. What am I missing? Due to large number of views, I have decided to offer a reward for…
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
22
votes
1 answer

Why is it called "open (or closed) recursion?

I found some explanations of open/closed recursion, but I do not understand why the definition contains the word "recursion", or how it compares with dynamic/static dispatching. Among the explanations I found, there are: Open recursion. Another…
13
votes
1 answer

dynamic modifier in functions in Swift

According to Apple : When you mark a member declaration with the dynamic modifier, access to that member is always dynamically dispatched. Because declarations marked with the dynamic modifier are dispatched using the Objective-C runtime, they’re…
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
11
votes
2 answers

Nested singleton class method lookup

First of all, I understand that this question has no application in real world, I'm just curious. Imagine we have a class with a singleton method: class Foo def self.bar end end If we call Foo.bar, it will first search for a method in a…
Dima Knivets
  • 2,418
  • 7
  • 28
  • 40
11
votes
3 answers

Dynamic Dispatch without Visitor Pattern

Problem I am working with an already existing library, to the source code of which I do not have access. This library represents an AST. I want to copy parts of this AST, but rename references to variables in the process. Since there can be a…
9
votes
3 answers

What is the difference between Binding and Dispatching in Java?

There are too many associated names: Early and Late Binding, Static and Dynamic Dispatch, Runtime vs. Compile-time Polymorphism, etc. that I don't understand the difference. I found a clear explanation, but is it correct? I'll paraphrase…
9
votes
2 answers

Covariant return type and type conversion

s->duplicate() returns an object of type Box*, but I'm getting an error initializing it with Box*. It looks like it's being converted back to Shape*. What is the point of having covariant return types if it's converted back to the base class…
template boy
  • 10,230
  • 8
  • 61
  • 97
8
votes
2 answers

Decompile Scala code: why there are two overridden methods in the derived class?

Decompile Scala code: why there are two overridden methods in the derived class? class A { private var str: String = "A" val x: A = this override def toString(): String = str def m1(other: AnyRef): AnyRef = { println("This is…
CodingNow
  • 998
  • 1
  • 11
  • 23
8
votes
1 answer

Is the Visitor Pattern the fastest way to differentiate parameter types in C++?

Is the Visitor Pattern the fastest way to accomplish method parameter type identification (effectively single dispatch on a parameter, not a member's class) in C++? I might know the exact method(s) I want to invoke on elements of not-yet-know…
Jeff
  • 3,475
  • 4
  • 26
  • 35
7
votes
2 answers

How to register typing.Callable with Python @singledispatch?

Background Suppose I am to implement a simple decorator @notifyme that prints a message when the decorated function is invoked. I would like the decorator to accept one argument to print a customized message; the argument (along with the parentheses…
Yang Hanlin
  • 474
  • 1
  • 6
  • 18
7
votes
3 answers

Is Fragile Base Class the only reason why "inheritance breaks encapsulation"?

As the Gang of Four states it in "Design Patterns": "it's often said that 'inheritance breaks encapsulation'", paraphrasing Snyder in "Encapsulation and Inheritance in Object-Oriented Programming Languages". However, each time I read that…
7
votes
1 answer

Redeclaring members in an extension hides the original member *sometimes*. Why?

By chance, I discovered that you can do this without the compiler complaining: extension Date { var timeIntervalSinceNow: TimeInterval { return 1000 } } What's weirder, is that this actually evaluates to…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1
2 3
11 12