# Is there a way to get a function's method signatures as data?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-get-a-functions-method-signatures-as-data/21402>\
**Category:** General Usage\
**Created:** [March 2, 2019, 7:52pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-a-functions-method-signatures-as-data/21402 "2019-03-02T19:52:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [March 2, 2019, 7:52pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-a-functions-method-signatures-as-data/21402/1 "2019-03-02T19:52:59Z")

</div>

I would like to write something that looks at a function’s method signatures and builds a command-line interface based on that. I realize it is easy to get a function’s definition with a macro, but I don’t know if there is a way to do it after the function already exists.

I know Julia has this somehow internally because of the `methods` function, but I don’t know how to get at it. If there’s no way, I guess I’ll settle for the macro approach, but I’d like to do it a little fancier.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 2, 2019, 7:55pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-a-functions-method-signatures-as-data/21402/2 "2019-03-02T19:55:59Z")

</div>

I’m using this in a (yet to be, maybe never released) project, but unfortunately it’s not typesafe. If you can make it typesafe (and ideally Base internals free?) I’ll gladly incorporate it 😃

```julia
getsig(f::Function) = map(b -> b.sig, methods(f).ms)
function argTypes(f::Function)
    filtered = filter(x -> !(x isa UnionAll) && !(Any in x.parameters), getsig(f))
    return map(x -> x.parameters[2:end], filtered)
end

```

It basically gets all known signatures for a given `Function` and does some filtering on argument types, to remove any calls with `Any` or `UnionAll`s.

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [March 2, 2019, 8:00pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-a-functions-method-signatures-as-data/21402/3 "2019-03-02T20:00:06Z")

</div>

ah! so this works with `methods`. I somehow thought this was just a pretty-printer. I guess I should have realized.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [October 16, 2019, 9:10pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-a-functions-method-signatures-as-data/21402/4 "2019-10-16T21:10:41Z")

</div>

> [@Sukera](#):
>
> x → !(x isa UnionAll)

can you give me a function definition where the signature is a UnionAll?  
and why did you filter out the UnionAlls?  
and why did you filter out the signatures containing Any in its parameters?

Thanks
