Julia Macro Resolution

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"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,551评论 5 6
  • 写一些可以记录人生的东西
    宣泄书阅读 76评论 0 0
  • 本不想写猫老师,因为猫群小伙伴对猫老师的崇拜及赞美已经多得不只一丢丢,我想也不差我这一点。但是仔细一想我可写的几个...
    tiaoyuema阅读 335评论 0 0
  • 以前我讲过地漏效应,地漏效应就是山寨币崩盘的时候水都会流入地漏,地漏就是比特币,也就是山寨替比特囤水囤钱 必须效应...
    比特观点阅读 334评论 0 0