# Method to Dict

**URL:** <https://discourse.julialang.org/t/method-to-dict/95840>\
**Category:** General Usage\
**Tags:** methods\
**Created:** [March 10, 2023, 4:44am UTC](https://discourse.julialang.org/t/method-to-dict/95840 "2023-03-10T04:44:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 10, 2023, 4:44am UTC](https://discourse.julialang.org/t/method-to-dict/95840/1 "2023-03-10T04:44:30Z")

</div>

Is it possible to convert a method to a dictionary (or similar structure) with entries:

```
    Argument_Name => Argument_Type

```

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [March 10, 2023, 6:57am UTC](https://discourse.julialang.org/t/method-to-dict/95840/2 "2023-03-10T06:57:30Z")

</div>

I was just curious if/how this would work, here are my findings

- methods are instances of a `Method` type
- with `methods(f)` we can access all methods of a function `f` (in a `Base.MethodList`)
- the instances of the method (all ?) seem to have the fields `sig` (signature?) and `slot_syms` (names of the arguments?) which could be massaged into a dictionary
- the argument names contain some other stuff and would need to be parsed
- the signature has the function type itself as the first element

Example:

```julia
# Ex 1

julia> sortmethod1 = methods(sort)[1]
sort(r::AbstractUnitRange) in Base at range.jl:1379

julia> sortmethod1.slot_syms
"#self#\0r\0"

julia> sortmethod1.sig
Tuple{typeof(sort), AbstractUnitRange}

# Ex 2

julia> plusmethod = last(methods(+))
+(a, b, c, xs...) in Base at operators.jl:591

julia> plusmethod.slot_syms
"#self#\0a\0b\0c\0xs\0"

julia> plusmethod.sig
Tuple{typeof(+), Any, Any, Any, Vararg{Any}}

```

I’m really not sure what caveats there might be to this approach or if there are some convenience functions to do this 😅 it’s fun to do a bit of “reverse-engineering” though.

EDIT: Here is a quick and dirty function that answers your actual question (going for a list of `Pair`s to preserve the argument order – an `OrderedDict` might also be convenient):

```julia
function get_method_info(method)
    argNames = split(method.slot_syms, "\0")[2:end-1] .|> string
    argTypes = method.sig.parameters[2:end]
    return [n => T for (n, T) in zip(argNames, argTypes)]
end

julia> get_method_info(plusmethod)
4-element Vector{Pair{String}}:
  "a" => Any
  "b" => Any
  "c" => Any
 "xs" => Vararg{Any}

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 10, 2023, 10:34am UTC](https://discourse.julialang.org/t/method-to-dict/95840/3 "2023-03-10T10:34:08Z")

</div>

Thanks Sevi  
Interesting that its not more easily accessible. You have to split strings etc
