Finding all applicable Methods for Signature

Hi there. Im looking to programmatically create a list of all Methods of a given function f whose signature is compatible with a given argument list args, that is, a list of all methods that could be triggered by invoke(f, argtypes, args...) with the right argtypes.

For example, given the definition

f(i)=1
f(i::Number)=2
f(i::Int) = 3
f(i::String)=4

and an argument that is Int, I want a list containing the Methods for Any,Number and Int, but NOT the Method for String. So essentially I want this:

julia> applicableMethods(f,Tuple{Int})
# 3 methods for generic function "f":
[1] f(i::Int64) in Main at REPL[9]:1
[2] f(i::Number) in Main at REPL[8]:1
[3] f(i) in Main at REPL[7]:1

The documentation for the methods function claims to do just that, but calling methods(f,Tuple{Int}) returns a list with just one Method, the most specific one. (Is that a bug or intended?)

If there was a way to check a method against the given type signature, like applicable(method, args...) (which does not work on methods), I could filter the whole Method list, but I have not found an easy way of doing so, as this typechecking can be arbitrarily complicated with typeparameters or “slurping arguments”.

Any help appreciated!

1 Like