# Behaviour of dispatch with args

**URL:** <https://discourse.julialang.org/t/behaviour-of-dispatch-with-args/23826>\
**Category:** General Usage\
**Created:** [May 3, 2019, 5:53pm UTC](https://discourse.julialang.org/t/behaviour-of-dispatch-with-args/23826 "2019-05-03T17:53:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [May 3, 2019, 5:53pm UTC](https://discourse.julialang.org/t/behaviour-of-dispatch-with-args/23826/1 "2019-05-03T17:53:39Z")

</div>

I don’t understand the dispatching of the functions defined below

```julia
f(a::Int, x::AbstractArray, t = "test") = "array"
f(a::Int, x::AbstractRange{<:Integer}, t) = "range"
f(args...) = f(2, args...)

```

`f(1:10)` and `f(2, 1:10)` result in `"range"`, although t is not an optional argument and I would expect that therefore `f(a::Int, x::AbstractArray, t = "test")` is called.

Interestingly, `@which f(2, 1:10)` outputs the expected `f(a::Int64, x::AbstractArray)`

Any ideas, why this happens?

P.S.:  
`methods(f)` results in

```julia
f(a::Int64, x::AbstractRange{#s1} where #s1<:Integer, t) in Main at In[1]:2
f(a::Int64, x::AbstractArray) in Main at In[1]:1
f(a::Int64, x::AbstractArray, t) in Main at In[1]:1
f(args...) in Main at In[1]:3

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [May 3, 2019, 6:16pm UTC](https://discourse.julialang.org/t/behaviour-of-dispatch-with-args/23826/2 "2019-05-03T18:16:01Z")

</div>

That does seem wrong. Here’s a smaller example:

```julia
julia> f(x::AbstractArray, t = "test") = "array"
f (generic function with 2 methods)

julia> f(x::AbstractRange{<:Integer}, t) = t
f (generic function with 3 methods)

julia> f(1:10)
"test"

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [May 3, 2019, 6:26pm UTC](https://discourse.julialang.org/t/behaviour-of-dispatch-with-args/23826/4 "2019-05-03T18:26:00Z")

</div>

This is [https://github.com/JuliaLang/julia/issues/7357#issuecomment-46766981](https://github.com/JuliaLang/julia/issues/7357#issuecomment-46766981).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 3, 2019, 6:41pm UTC](https://discourse.julialang.org/t/behaviour-of-dispatch-with-args/23826/5 "2019-05-03T18:41:22Z")

</div>

> [@tkoolen](#):
>
> `f(x::AbstractArray, t = "test") = "array"`

This is equivalent to

```julia
f(x::AbstractArray, t) = "array"
f(x::AbstractArray) = f(x::AbstractArray, "test")

```

So `f(1:10)` calls the second method, which calls `f(1:10, "test")`, which calls your `AbstractRange` method.

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [May 3, 2019, 6:52pm UTC](https://discourse.julialang.org/t/behaviour-of-dispatch-with-args/23826/6 "2019-05-03T18:52:50Z")

</div>

Thanks @tkoolen and @stevengj for these quick answers.  
Now I can certainly find a solution for my case 🙂
