如何检索 Julia 宏的方法?

问题描述 投票:0回答:1

在 Julia 中,

methods
函数可用于检索函数的方法。

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

宏也可以有多种方法。

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"
""

但是,

methods
函数似乎不适用于宏,因为 Julia 首先调用宏,因为它们不需要括号。

julia> methods(@g)
ERROR: MethodError: no method matching @g()
Closest candidates are:
  @g(::String) at REPL[2]:2
  @g(::Int64) at REPL[1]:2

我尝试使用

Expr
会话来包含宏,但这不起作用。

julia> methods(:@g)
# 0 methods for generic function "(::Expr)":

如何检索宏的方法?

methods macros julia
1个回答
2
投票

我会在我的

@methods
中的模块(
MethodsMacro
)中放置一个通用宏(
~/.juliarc.jl
)以及以下行:
using MethodsMacro
。这样,您就可以在每次 Julia 会话中使用它,例如:

julia> module MethodsMacro                                               
                                                                    
       export @methods                                              
                                                                    
       macro methods(arg::Expr)                                     
           arg.head == :macrocall || error("expected macro name")   
           name = arg.args[1] |> Meta.quot                           
           :(methods(eval($name)))                                  
       end                                                          
                                                                    
       macro methods(arg::Symbol)                                   
           :(methods($arg)) |> esc                                  
       end                                                          
                                                                    
       end                                                          
MethodsMacro                                                             
                                                                    
julia> using MethodsMacro                                                
                                                                    
julia> @methods @methods                                            
# 2 methods for macro "@methods":                                   
@methods(arg::Symbol) at REPL[48]:12                                
@methods(arg::Expr) at REPL[48]:6                                   
                                                                    
julia> f() = :foo; f(x) = :bar                                      
f (generic function with 2 methods)                                 
                                                                    
julia> @methods f                                                   
# 2 methods for generic function "f":                               
f() at REPL[51]:1                                                   
f(x) at REPL[51]:1     

                                         
                                                                                                                           
© www.soinside.com 2019 - 2024. All rights reserved.