# Question about the selection mechanism of multiple dispatch

**URL:** <https://discourse.julialang.org/t/question-about-the-selection-mechanism-of-multiple-dispatch/11175>\
**Category:** General Usage\
**Created:** [May 27, 2018, 2:22am UTC](https://discourse.julialang.org/t/question-about-the-selection-mechanism-of-multiple-dispatch/11175 "2018-05-27T02:22:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 27, 2018, 2:22am UTC](https://discourse.julialang.org/t/question-about-the-selection-mechanism-of-multiple-dispatch/11175/1 "2018-05-27T02:22:53Z")

</div>

Hi guys!

I am having problems to understand the mechanism of multiple dispatch. The documentation has the following text:

> When a function is applied to a particular tuple of arguments, the most specific method applicable to those arguments is applied. Thus, the overall behavior of a function is a patchwork of the behaviors of its various method definitions. If the patchwork is well designed, even though the implementations of the methods may be quite different, the outward behavior of the function will appear seamless and consistent.

However, let me see if I understood correctly.

Let’s say I have the following type:

```julia
T_ECIs = Union{Type{Val{:GCRF}},
               Type{Val{:J2000}},
               Type{Val{:TOD}},
               Type{Val{:MOD}},
               Type{Val{:TEME}}}

```

And then I defined the two following functions:

```julia
function rECItoECI(T_ECIo::T_ECIs, T_ECId::T_ECIs, JD_UTC::Number)
...
end

```

```julia
function rECItoECI(T_ECIo::Type{Val{:TEME}}, T_ECId::Type{Val{:TEME}}, JD_UTC::Number)
...
end

```

In a simplified example, I saw that if I call `rECItoECI(Val{:TEME}, Val{:TEME}, 0)` then the second function is selected. However, is it assured that this will **always** happen? I think I am confusing what “most specific method” means.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 27, 2018, 3:39am UTC](https://discourse.julialang.org/t/question-about-the-selection-mechanism-of-multiple-dispatch/11175/2 "2018-05-27T03:39:06Z")

</div>

Well, somebody could later add a method such as:

```julia
function rECItoECI(T_ECIo::Type{Val{:TEME}}, T_ECId::Type{Val{:TEME}}, JD_UTC::Integer)
...

```

which would be more specific that with `JD_UTC::Number`,  
so the dispatch would go to that method instance.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 27, 2018, 8:41pm UTC](https://discourse.julialang.org/t/question-about-the-selection-mechanism-of-multiple-dispatch/11175/3 "2018-05-27T20:41:18Z")

</div>

Thanks!

But without this definition, the second one will always be called right?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 27, 2018, 9:11pm UTC](https://discourse.julialang.org/t/question-about-the-selection-mechanism-of-multiple-dispatch/11175/4 "2018-05-27T21:11:24Z")

</div>

Yes. A type `T` is always more specific than `Union{T, X}` for some other type `X`, so a method defined with `::T` as its argument will be more specific than one with `::Union{T, X}` (assuming no change in the other arguments).

A few examples:

- `foo(::T)` is more specific than `foo(::Union{T, X})`
- `foo(::T, ::X)` is more specific than `foo(::Union{T, X}, ::X)`
- `foo(::T, ::X)` is more specific than `foo(::T, ::Union{T, X})`
- `foo(::T, ::X)` is more specific than `foo(::Union{T, X}, ::Union{T, X})`

But there are ambiguous cases:

- `foo(::Union{T, X}, ::X)` is ambiguous with (neither more nor less specific than) `foo(::T, ::Union{T, X})`

If there is no unique most-specific method, then Julia will throw an ambiguity error, so there’s no need to worry about one method being arbitrarily preferred.
