# Properly query methods of a function?

``````    sig_params(t::UnionAll) = sig_params(t.body)
sig_params(t::DataType) = t.parameters[2:end]
abstract type AT2{T<:Real} end
f(x::AT2{Float64})  = "AT2{Float64}"
f(x::T) where T<:AT2 = "T<:AT2"

julia> methods(f)
# 2 methods for generic function "f":
[1] f(x::AT2{Float64}) in Main at REPL[4]:1
[2] f(x::T) where T<:AT2 in Main at REPL[5]:1

julia> sig = methods(f).ms[2].sig
Tuple{typeof(f),T} where T<:AT2

julia> methods(f,sig_params(sig))
# 2 methods for generic function "f":
[1] f(x::AT2{Float64}) in Main at REPL[4]:1
[2] f(x::T) where T<:AT2 in Main at REPL[5]:1

julia> code_lowered(f,sig_params(sig))
2-element Array{Core.CodeInfo,1}:
CodeInfo(
1 â”€     return "AT2{Float64}"
)
CodeInfo(
1 â”€     return "T<:AT2"
)

``````

what is the proper signature to query only the second method â€ś[2] f(x::T) where T<:AT2â€ť ?

one more question, the results of methods and code_lowered, do they correspond one to one always with proper order?

thanks

``````julia> methods(f, Tuple{AT2{Int}})
# 1 method for generic function "f":
[1] f(x::T) where T<:AT2 in Main at REPL[3]:1
``````

And I donâ€™t believe you can get method to return only the less specific one.

Why do you need this? Why canâ€™t you just use the current result from `methods`?

2 Likes