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

#1

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.

#2

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

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

4 Likes
#3

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