How to get all methods using certain type as their first parameter


#1

How to get all methods using certain type as their first parameter:

type A 
 ...
end

function get_property(a::A) ... end
function process(a::A, ...) ... end
function make(a::A, ...) ... end

So it would be possible to get get_property, process, make methods? This is useful
for exploring APIs in runtime, in OOP world this is quite simple, not sure how to do this in Julia.


#2

methodswith() should work for you:

julia> methodswith(String)
67-element Array{Method,1}
==(a::String, b::String) in Base at strings/string.jl:82            
abspath(a::String) in Base.Filesystem at path.jl:269            
ascii(s::String) in Base at strings/util.jl:478                           
chomp(s::String) in Base at strings/util.jl:99                             
cmp(a::String, b::String) in Base at strings/string.jl:76             
codeunit(s::String, i::Integer) in Base at strings/string.jl:65      
convert(::Type{Array{UInt8,1}}, s::String) in Base at strings/string.jl:45 
...

#3

methodswith certainly works in the sense that it returns methods where a certain type occurs as a method argument (somewhere). However if - as the OP mentioned - the argument should be at the first position, some filtering is needed. Example:

struct A
    foo
end
function get_property(a::A) println("get_property") end
function process(i::Int, a::A) println("process") end
function make(a::A, j::Int) println("make") end

mm = methodswith(A)  # returns methods (process) where A is *not* at first position

# filter (I'm no expert, just looked at the structure and picked fields which seemed helpful)

# [--- digression (gather some background about objects)
mm |> typeof
mm[1] |> typeof
mm[1] |> typeof |> fieldnames
mm[1] |> dump
mm[1].sig |> typeof |> fieldnames
mm[1].sig.parameters                   # seems useable, 2nd elem is relevant
# ---]

mmf = [m for m in mm if m.sig.parameters[2] === A]   # bingo