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

**URL:** <https://discourse.julialang.org/t/how-to-get-all-methods-using-certain-type-as-their-first-parameter/4869>\
**Category:** General Usage\
**Created:** [July 15, 2017, 10:52am UTC](https://discourse.julialang.org/t/how-to-get-all-methods-using-certain-type-as-their-first-parameter/4869 "2017-07-15T10:52:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [July 15, 2017, 10:52am UTC](https://discourse.julialang.org/t/how-to-get-all-methods-using-certain-type-as-their-first-parameter/4869/1 "2017-07-15T10:52:25Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [July 15, 2017, 11:23am UTC](https://discourse.julialang.org/t/how-to-get-all-methods-using-certain-type-as-their-first-parameter/4869/2 "2017-07-15T11:23:09Z")

</div>

`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 
...

```

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [July 15, 2017, 12:56pm UTC](https://discourse.julialang.org/t/how-to-get-all-methods-using-certain-type-as-their-first-parameter/4869/3 "2017-07-15T12:56:48Z")

</div>

`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:

```julia
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

```
