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"
网友评论