Questions tagged [multiple-dispatch]

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.

106 questions
321
votes
20 answers

Python function overloading

I know that Python does not support method overloading, but I've run into a problem that I can't seem to solve in a nice Pythonic way. I am making a game where a character needs to shoot a variety of bullets, but how do I write different functions…
Bullets
  • 3,251
  • 3
  • 15
  • 6
49
votes
4 answers

Multiple dispatch in C++

I am trying to understand what multiple dispatch is. I read a lot of various texts but I still have no idea what multiple dispatch is and what it is good for. Maybe the thing I am missing is piece of code using multiple dispatch. Please, can you…
Martin
  • 1,473
  • 2
  • 12
  • 20
40
votes
2 answers

How to write "good" Julia code when dealing with multiple types and arrays (multiple dispatch)

OP UPDATE: Note that in the latest version of Julia (v0.5), the idiomatic approach to answering this question is to just define mysquare(x::Number) = x^2. The vectorised case is covered using automatic broadcasting, i.e. x = randn(5) ; mysquare.(x).…
Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
35
votes
7 answers

What's the difference between Polymorphism and Multiple Dispatch?

...or are they the same thing? I notice that each has its own Wikipedia entry: Polymorphism, Multiple Dispatch, but I'm having trouble seeing how the concepts differ. Edit: And how does Overloading fit into all this?
raldi
  • 21,344
  • 33
  • 76
  • 86
32
votes
8 answers

Is C# a single dispatch or multiple dispatch language?

I'm trying to understand what single and multiple dispatch are, exactly. I just read this: http://en.wikipedia.org/wiki/Multiple_dispatch And from that definition is seems to me that C# and VB.Net are multiple-dispatch, even though the choice of…
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
17
votes
1 answer

How does Julia implement multimethods?

I've been reading a bit from http://c2.com/cgi/wiki?ImplementingMultipleDispatch I've been having some trouble finding reference on how Julia implements multimethods. What's the runtime complexity of dispatch, and how does it achieve it?
math4tots
  • 8,540
  • 14
  • 58
  • 95
12
votes
2 answers

Is there any difference between multimethod and multipledispatch?

I would like to use overloading in Python. I know it's not possible by design (Python is dynamically typed language), there is quite good thread here on this topic. That's why I could use something like multiple dispatch (multimethods). And I'm…
Nerxis
  • 3,452
  • 2
  • 23
  • 39
12
votes
1 answer

How to provide a non-slurpy array or named array from the command line?

First of all: raku (perl6) is amazing. And so is Cro. It only took a week-end to fall in love. However now I stumble over something that must be extremely simple. If I use a slurpy parameter in a multiple dispatch MAIN this is recognized and works…
10
votes
3 answers

Can one encode binary functions between types in OCaml?

I am wondering if it is possible to build something similar to multiple dispatch in OCaml. To do that, I tried to make an explicit type for the input signature of a multimethod. As an example, I define a number type type _ num = | I : int -> int…
user3240588
  • 1,252
  • 9
  • 16
10
votes
1 answer

What method did Julia use?

I have defined the following variable julia> X = (1:10) * ones(1,10) which defines a matrix with each row equals to the same number and the numbers in the column increasing from 1 to 10 by 1. I want to know which method used Julia for the function…
dapias
  • 2,512
  • 3
  • 15
  • 23
9
votes
1 answer

How to avoid massive amounts of boilerplate in Julia with new types?

I'm considering writing a type similar to those defined in NamedArrays and Images. Let's say I just want essentially an Array with a piece of metadata, say a user-friendly name that I'll write at the top of a file when I write the array to disk.…
Philip
  • 7,253
  • 3
  • 23
  • 31
9
votes
2 answers

Use invokedynamic to implement multiple dispatch

I wondered if Java7's new invokedynamic bytecode instruction could be used to implement multiple dispatch for the Java language. Would the new API under java.lang.invoke be helpful to perform such a thing? The scenario I was thinking about looked as…
Matt
  • 868
  • 8
  • 12
8
votes
2 answers

Dispatching on arguments after the slurping operator (args...) in julia

How would you implement a function like this: function foo(a,b...,c) println(a,b,c) end foo(2,3,3,"last") => a = 2 , b = (3,3) , c = "last" I cannot use something like: function foo(a,b...) c = b[end] println(a,b,c) end Because I…
ailuj
  • 83
  • 4
8
votes
1 answer

Generic dispatch with Symbols

I was wondering if there was a way to use Symbols for multiple dispatch, but also include a "catch-all method". i.e. something like function dispatchtest{alg<:Symbol}(T::Type{Val{alg}}) println("This is the generic dispatch. The algorithm is…
Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81
7
votes
2 answers

Can I choose between Perl 6 multis that have no parameters?

I can choose a multi based on some non-argument value but I have to have at least one argument so I can kludge the where in there: our $*DEBUG = 1; debug( 'This should print', 'Phrase 2' ); $*DEBUG = 0; debug( 'This should not print' ); multi…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
1
2 3 4 5 6 7 8