Why does this method signature turns 2 methods instead of 1?

julia> f(x::Int) = "A"
f (generic function with 2 methods)

julia> f(x::Union{Int,Float64}) = "B"
f (generic function with 3 methods)

julia> methods(f,Tuple{Union{Int,Float64}})
# 2 methods for generic function "f":
[1] f(x::Int64) in Main at none:1
[2] f(x::Union{Float64, Int64}) in Main at none:1


The first one is more specific and so takes precedence when the argument is of type Int.

when it comes to dispatch, you are right it is more specific but when it comes to query of methods, the second is more specific?

How can I get the latter one?

I’m not sure what you mean by “query of methods”. That sounds like dispatch to me.

methods(f,Tuple{Union{Int,Float64}}) → queries the methods of the function f with the signature.

methods returns both. I believe the object it returns is an iterator that you can iterate through.

1 Like