Behaviour of dispatch with args

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

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?

methods(f) results in

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

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

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)

This is

1 Like

This is equivalent to

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.

1 Like

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