In Julia, the methods function may be used to retrieve the methods of a function.
julia> f(::Int) = 0
f (generic function with 1 method)
julia> f(::String) = ""
f (generic function with 2 methods)
julia> methods(f)
# 2 methods for generic function "f":
f(::String) in Main at REPL[1]:1
f(::Int64) in Main at REPL[0]:1
Macros can also have multiple methods.
julia> macro g(::Int)
0
end
@g (macro with 1 method)
julia> macro g(::String)
""
end
@g (macro with 2 methods)
julia> @g 123
0
julia> @g "abc"
""
However, the methods function does not seem to work on macros because Julia first calls the macro, due to the fact that they do not need parentheses.
julia> methods(@g)
ERROR: MethodError: no method matching @g()
Closest candidates are:
@g(::String) at REPL[2]:2
@g(::Int64) at REPL[1]:2
I tried using an Expression to contain the macro, but this did not work.
julia> methods(:@g)
# 0 methods for generic function "(::Expr)":
How can I retrieve the methods of a macro?
methods(eval(Symbol("@g")))worked for me, but there must be a cleaner method - Dan Getzeval):methods(Main.(Symbol("@g")))- Dan GetzMain.(Symbol("@g"))is deprecated, andgetfield(Main, Symbol("@g")should be used instead.) - Harrison Grodin