# Method dispatch for tuples

**URL:** <https://discourse.julialang.org/t/method-dispatch-for-tuples/5925>\
**Category:** Internals & Design\
**Tags:** question, tuple\
**Created:** [September 16, 2017, 6:38pm UTC](https://discourse.julialang.org/t/method-dispatch-for-tuples/5925 "2017-09-16T18:38:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 16, 2017, 6:38pm UTC](https://discourse.julialang.org/t/method-dispatch-for-tuples/5925/1 "2017-09-16T18:38:35Z")

</div>

I have the following definitions:

```julia
f(x::Tuple{Any, Any}) = "Tuple{Any, Any}"
f(x::NTuple{N}) where N = "NTuple{N}"

```

When I write `f((1,2))` the call satisfies both definitions. On Julia 0.6 the second definition is called but I do not understand why. Is this a bug or there is some explanation for this behavior?

I would assume that `MethodError` should be thrown as this call is ambiguous because:

```julia
julia> typeof((1,2)) <: Tuple{Any, Any}
true

julia> typeof((1,2)) <: NTuple{N} where N
true

```

but

```julia
julia> Tuple{Any, Any} <: NTuple{N} where N
false

julia> (NTuple{N} where N) <: Tuple{Any, Any}
false

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 16, 2017, 10:30pm UTC](https://discourse.julialang.org/t/method-dispatch-for-tuples/5925/2 "2017-09-16T22:30:28Z")

</div>

`NTuple` requires that all the inputs have the same type, so

```julia
julia> typeof((1,2)) <: NTuple{N} where N
true

julia> typeof((1,2.0)) <: NTuple{N} where N
false

```

Hence it is deemed “more specific,” as the lingo goes, and hence gets priority.

Of course this can be debated: why should the element type receive priority over the length? If you need to have manual control over this, you can always define a method for `::NTuple{2}` and have it do whichever one you want.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 17, 2017, 6:24am UTC](https://discourse.julialang.org/t/method-dispatch-for-tuples/5925/3 "2017-09-17T06:24:46Z")

</div>

Thank you. Now I understand that it is governed by `jl_type_morespecific`.

As a side observation: for `Array` dimension gets the priority over element type as `Array{T, 1} where T` is more specific than `Array{Int, N} where N` (so I would say it is the other way around than in `Tuple` although there is no one to one correspondence between those cases).

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 17, 2017, 7:54am UTC](https://discourse.julialang.org/t/method-dispatch-for-tuples/5925/4 "2017-09-17T07:54:08Z")

</div>

That seems like a reasonable question. Relatedly, I filed an [issue](https://github.com/JuliaLang/julia/issues/23740) so that we don’t forget to document these rules someday.
