美文网首页
Julia Macro Resolution

Julia Macro Resolution

作者: 我要走多远 | 来源:发表于2015-03-16 15:16 被阅读36次

Resolution

 module Foo
   x = "I am in Foo"
   macro resulation()
       :(x)
  end
 end

 julia> Foo.@resulation()
 "I am in Foo"

 julia> macroexpand(:( Foo.@resulation() ))
 :(Foo.x)
 x = "current"

 module Foo
   x = 100
   macro resulation()
     esc(:(x))
   end

   res = @resulation()
 end

 julia> x = "current"
 "current"

 julia> Foo.res
 100

 julia> Foo.@resulation()
 "current"

 macroexpand(:( Foo.@resulation() ))
 :x

So a variable in macro, if it is not in esc, it will be resolved in where it is defined.
If it is in esc, it will be resolved where it is called.

Resolution for arguments

You can resolve an argument by $(esc(exp))
eg:

 module Foo
   x = 100
   macro foo(exp)
       :($(esc(exp)))
 end
 end

 Foo.@foo( x )
 "current"

and you can do this by esc(:($(exp))) too.

 module Foo
   x = 100
   macro foo(exp)
      esc(:($(exp)))
   end
 end

 julia> Foo.@foo( x )
 "current"

相关文章

网友评论

      本文标题:Julia Macro Resolution

      本文链接:https://www.haomeiwen.com/subject/wisixttx.html