# Basic concept about multiple dispatch

**URL:** <https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270>\
**Category:** New to Julia\
**Created:** [September 21, 2018, 11:21am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270 "2018-09-21T11:21:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [September 21, 2018, 11:21am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/1 "2018-09-21T11:21:16Z")

</div>

hello, I found myself got confused about multiple dispatch. Take the function `rand` as an example: there’re a lot of method definitions across different files:

in `base/libc.jl`:

```julia
rand() = ccall(:rand, Cint, ())
rand(::Type{UInt32}) = ((rand() % UInt32) << 16) ⊻ (rand() % UInt32)
rand(::Type{Float64}) = rand(UInt32) / 2^32

```

and in `stdlib/Random/src/Random.jl`:

`rand(rng::AbstractRNG=GLOBAL_RNG, ::Type{X}=Float64) where {X} = rand(rng, Sampler(rng, X, Val(1)))`

now I try to inspect **which** method definition is in use when calling `rand()`, which should give a random number in [0,1):

```julia
julia> @which rand()
rand() in Random at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/Random/src/Random.jl:222

```

seems like the above definition in `Random.jl` is in use. Now the questions are:

1. why the plain `rand()` definition in l`ibc.jl` (i.e. the first one) **not** called?
2. how do Julia decide the priority of the methods to dispatch **_if_** two or more methods all match the call? It could happen say one method has no parameter, and the other has parameters all having default values.
3. what does `fun(::Type{X}=Float64) where {X}` means? why could the parameter has **no name**?

thanks.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 21, 2018, 11:28am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/2 "2018-09-21T11:28:08Z")

</div>

It’s not the same generic function:

```julia
julia> Base.Libc.rand                                                                                                                                                  
rand (generic function with 3 methods)                                                                                                                                 

julia> rand                                                                                                                                                            
rand (generic function with 56 methods)                                                                                                                                

```

which happen to have the same name but are not the same.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [September 21, 2018, 11:30am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/3 "2018-09-21T11:30:01Z")

</div>

wow!!! It gets even more confusing now!!!

how could functions having the same name come with different “branch” of multiple dispatches ???

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 21, 2018, 11:32am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/4 "2018-09-21T11:32:26Z")

</div>

Just like two different types can have the same name, say

```julia
module A
struct AB end
end
module B
struct AB end
end

```

so can functions

```julia
module A
f() = 1
end
module B
f() = 1
end

```

Check this thread:

> [@Two modules with the same exported function name but different signature](https://discourse.julialang.org/t/two-modules-with-the-same-exported-function-name-but-different-signature/15231):
>
> Hello, I have the following situation which I did not expected: defining a fucntion with the same name but different signature in two modules like this: ## Module RawTables function save(T::RawTable, name\_pref::String) ... end ## Module RawPlots function save(T::RawPlot, name\_pref::String) ... end and using them together results in WARNING: both RawPlots and RawTables export "save"; uses of it in module Main must be qualified I thought multiple dispatch is exactly good in being able to res…

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 21, 2018, 11:34am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/5 "2018-09-21T11:34:13Z")

</div>

Doesn’t really answer the question why the second is chosen:

```julia
julia> methods(Base.Libc.rand)
# 3 methods for generic function "rand":
[1] rand() in Base.Libc at libc.jl:374
[2] rand(::Type{UInt32}) in Base.Libc at libc.jl:376
[3] rand(::Type{Float64}) in Base.Libc at libc.jl:377

julia> methods(rand)
# 71 methods for generic function "rand":
...
[15] rand() in Random at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Random\src\Random.jl:222
...

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 21, 2018, 11:43am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/6 "2018-09-21T11:43:28Z")

</div>

Because that is what is being `using` in `Main` (i.e. the module of the REPL). In a module of yours you can choose which one you want:

```julia
julia> baremodule A
       using Random: rand
       Core.println(rand()) # uses Random.rand as at the REPL
       end                                                                                                                                                             
0.25909                                                                                                                                                                
Main.A                                                                                                                                                                 

julia> baremodule b
       using Base.Libc: rand
       Core.println(rand()) # uses Base.Libc.rand
       end                                                                                                                                                             
1408058198                                                                                                                                                             
Main.b                                                                                                                                                                 

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [September 21, 2018, 11:50am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/7 "2018-09-21T11:50:38Z")

</div>

could u be nice enough to point to where could I find the code of that `using` in module `Main`? (actually I don’t know which file corresponds to `Main` module).

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 21, 2018, 12:06pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/8 "2018-09-21T12:06:51Z")

</div>

Good question. Actually, it’s slightly different than I thought. The `rand` generic function is defined in Base:

> <https://github.com/JuliaLang/julia/blob/ce6454642ca2f6075623179c6804ea6c78d1fdb5/base/sysimg.jl#L325>

then imported into Random, where the actual methods are then defined:

> <https://github.com/JuliaLang/julia/blob/ce6454642ca2f6075623179c6804ea6c78d1fdb5/stdlib/Random/src/Random.jl#L16>

Thus it makes sense that `@which rand` returns `Base`.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [September 21, 2018, 12:13pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/9 "2018-09-21T12:13:37Z")

</div>

it’s quite a way to define methods … (need to learn).  
ah … so, there’s **_no_** such a file for module `Main`?

thus, having solved question 1, what about questions 2 and 3?

> 1. how do Julia decide the priority of the methods to dispatch _ **if** _ two or more methods all match the call? It could happen say one method has no parameter, and the other has parameters all having default values.
> 2. what does `fun(::Type{X}=Float64) where {X}` means? why could the parameter has **no name**?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 21, 2018, 12:25pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/10 "2018-09-21T12:25:22Z")

</div>

I don’t know where main is defined, maybe in base/client.jl.

1. it uses the most specific method. See [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/) and watch [JuliaCon 2018 | Subtyping made friendly | Francesco Zappa Nardelli - YouTube](https://youtu.be/Y95fAipREHQ).

2. `fun(::Type{X}=Float64) where {X} = X` will return the Type, defaulting to Float64.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 21, 2018, 12:43pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/11 "2018-09-21T12:43:10Z")

</div>

Note that `rand` is porbably one of the more complicated functions with respect to it’s distribution in source files. The reason is that although most random number stuff is in the `Random` stdlib `rand` itself is considered to be so essential that it has been decided to make it available in base julia.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 21, 2018, 12:45pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/12 "2018-09-21T12:45:16Z")

</div>

Also to 2., it can throw an ambiguity error in case it doesn’t know what to do. (See for example: [Example Radioactive Decay (Numbers with uncertainties) not working in Julia 1.0 · Issue #26 · SciML/SciMLTutorials.jl · GitHub](https://github.com/JuliaDiffEq/DiffEqTutorials.jl/issues/26))

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [September 22, 2018, 12:55am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/13 "2018-09-22T00:55:13Z")

</div>

could u please give me an example that throws “ambiguity error”? In the following, no error was thrown, and seems like the first method can never be used:

```julia
julia> function f()
           println("000")
       end
f (generic function with 1 method)

julia> function f(a=1)
           println("111")
       end
f (generic function with 2 methods)

julia> methods(f)
# 2 methods for generic function "f":
[1] f() in Main at none:2
[2] f(a) in Main at none:2

julia> f()
111

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 22, 2018, 5:17am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/14 "2018-09-22T05:17:07Z")

</div>

You should read [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#man-ambiguities-1), which indeed has such an example. If you make it through that section of the docs you’ll also find [this](https://docs.julialang.org/en/v1/manual/methods/#Note-on-Optional-and-keyword-Arguments-1) and understand why the first method you define is not called.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [September 22, 2018, 10:33am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/15 "2018-09-22T10:33:19Z")

</div>

following [this] manual, the following function

```julia
function f(a=1)
  println("111")
end

```

should translate into

```julia
function f(a)
  println("111")
end

f() = f(1)

```

however, the definition of `f() = f(1)` is **duplicated** , i.e. there was **another** defintion of `f()` **already**! The problem is it does **not** throw any error and the earlier definition is simply **overrided**?

as a simple test, I try the following:

```julia
julia> g() = 1
g (generic function with 1 method)

julia> g() = 2
g (generic function with 1 method)

```

and it’s the same: the **redefinition** of `g()` **_gave no error nor warning_**. Is it a desired behavior?

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [September 22, 2018, 11:55am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/16 "2018-09-22T11:55:35Z")

</div>

> [@tomtom](#):
>
> the **redefinition** of `g()` _ **gave no error nor warning** _ . Is it a desired behavior?

In practice redefinition never happens as one usually uses multiple dispatch with respect to all argument types. But redefinition is rather useful in interactive environments as REPL and Jupyter notebooks. So I see that as a desired behavior.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [September 22, 2018, 1:21pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/17 "2018-09-22T13:21:50Z")

</div>

hm… I think, as a matter of safety, redefinition of a function should throw a warning.

We could intentionally suppress the warning if we want. But the warning would be a very good alert.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 22, 2018, 4:41pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/18 "2018-09-22T16:41:05Z")

</div>

A warning is only thrown when redefining a method inside a package. It was thought that for interactive development the warnings were too noisy.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 23, 2018, 7:09am UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/19 "2018-09-23T07:09:25Z")

</div>

> [@tomtom](#):
>
> I think, as a matter of safety, redefinition of a function should throw a warning

It does — as @mauro3 said above — for redefinitions inside packages (modules). The lesson from this is that one should develop code inside packages, using Revise.jl.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 28, 2018, 1:14pm UTC](https://discourse.julialang.org/t/basic-concept-about-multiple-dispatch/15270/20 "2018-09-28T13:14:32Z")

</div>

A bit late, but here is a minimal example with an ambiguity error:

```julia
f(x::Int, y::Any) = println("int")
f(x::Any, y::String) = println("string")
f(3, "test")

```

will give you

```julia
MethodError: f(::Int64, ::String) is ambiguous. Candidates:
  f(x, y::String) in Main at In[15]:2
  f(x::Int64, y) in Main at In[18]:1
Possible fix, define
  f(::Int64, ::String)

```
