A technique for polymorphic invocation of methods based on the types of many (or all) arguments. Compare to single-dispatch, used in common OO languages, where methods can only be polymorphic in the first argument -- the runtime resolution of a.doSomething(x, y, z) depends only on the type of a.
Questions tagged [multiple-dispatch]
106 questions
5
votes
1 answer
How to do multiple dispatch on interface in C#?
Suppose I have the following class structure:
Page
The Page, StaticPage and DynamicPage interfaces are to be implemented by the clients. They provide various data depending on the page type (static or dynamic), which is used to render the page by…

proskor
- 1,382
- 9
- 21
4
votes
3 answers
How to distinguish different objects in the same struct using Symbol in Julia?
As mentioned in the title, I want to use multiple-dispatch to assign different behaviors of the same struct, distinguished by Symbol. The struct can be constructed as follows:
struct AbstractAlgorithm
algorithmName::String
…

PokeLu
- 767
- 8
- 17
4
votes
1 answer
Imported function lacks method defined in another module
I have a module A that describes a framework. Then, a couple of modules (here just B) that implement the framework. Finally, I want C to use any of the implementations.
However, running the following example:
module A
abstract type Ab end
…

Tanj
- 442
- 2
- 9
4
votes
0 answers
Is it possible to use functools.singledispatch with composite/nested/container types in Python?
Edit 2022-08-30: perhaps with the introduction of variadic generics (PEP-646) in Python 3.11, dispatch with composite types may become possible.
I wonder whether the following can be achieved,
and if so, without requiring much extra code:
from…

jrbergen
- 660
- 5
- 16
4
votes
2 answers
Efficient way to implement multiple dispatch for many similar functions
I am writing some software that involves a library of various functional forms of a quantity. I want to leverage Julia's multiple dispatch, but want to know if there's a more efficient way to implement this procedure.
Consider, for example, a…

diatomicDisaster
- 486
- 1
- 6
- 13
4
votes
3 answers
Simple way to do multiple dispatch in python? (No external libraries or class building?)
I'm writing a throwaway script to compute some analytical solutions to a few simulations I'm running.
I would like to implement a function in a way that, based on its inputs, will compute the right answer. So for instance, say I have the following…

dylanjm
- 2,011
- 9
- 21
4
votes
2 answers
How to use default parameter with multipledispatch?
I'm overloading functions with default parameter using multipledispatch (based on this answer)
from multipledispatch import dispatch
class TestExampleTest(AbstractTest):
@dispatch(ClassOne, bool, bool)
def function(self, my_class, a=True,…

Guy
- 46,488
- 10
- 44
- 88
4
votes
2 answers
Is there a difference between fun(n::Integer) and fun(n::T) where T<:Integer in performance/code generation?
In Julia, I most often see code written like fun(n::T) where T<:Integer, when the function works for all subtypes of Integer. But sometimes, I also see fun(n::Integer), which some guides claim is equivalent to the above, whereas others say it's less…

Sundar R
- 13,776
- 6
- 49
- 76
4
votes
2 answers
julia type unstable when accessing field from abstract type
I have a question about type instability when accessing the fields of an
abstract type (in julia v0.6).
Suppose I have a type hierarchy, all of which share the same instance variable.
I know that it is both type-unstable and not-guaranteed to be…

NHDaly
- 7,390
- 4
- 40
- 45
4
votes
2 answers
Julia: Efficiency of Multiple Parameters
According to the "performace tips" section of the Julia manual it is not advisable to go crazy with multiple dispatch. I've run into a situation where it seems I need 3 parameters for a type I'm defining. This is related to my question about using…

mv3
- 469
- 5
- 16
4
votes
1 answer
Dispatching on functions in Julia v0.5+
According to the changelog for Julia 0.5,
Each function and closure now has its own type.
Does this mean it's now possible to provide more detailed information to a higher-order function, e.g. foo(bar :: Function{Float64}) = ..., as opposed to…

Arets Paeglis
- 3,856
- 4
- 35
- 44
3
votes
1 answer
Multiple dispatch for the constructor method in Julia
I have written a struct and I want to have a constructor method that supports both vector and tuple input for the argument barrierPositions.
struct MapInfo
mapSize::Tuple{Int64, Int64}
flagPosition::Tuple{Int64, Int64}
…

PokeLu
- 767
- 8
- 17
3
votes
1 answer
Is Fortran a multiple dispatch programming language?
Does the INTERFACE statement in Fortran make it a programming language officially implementing multiple dispatch? (I ask because the Wikipedia article linked does not feature Fortran among its seemingly comprehensive listing of example programming…

wanderinganon
- 67
- 4
3
votes
2 answers
Abstract typing and multiple dispatch for functions in julia
I want to have objects interact with specific interactions depending on their type.
Example problem: I have four particles, two are type A, and 2 are type B. when type A's interact I want to use the function
function interaction(parm1, parm2)
…

Charlie Crown
- 1,071
- 2
- 11
- 28
3
votes
1 answer
Object, roles and multiple dispatch
I'm trying to use multiple dispatch to overload and use methods within composed classes. Here's the implementation:
role A {
has $!b;
submethod BUILD( :$!b ) {}
multi method bar () {
return $!b;
}
}
class B does A {
…

jjmerelo
- 22,578
- 8
- 40
- 86