-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Symbols passed to macros in a module improperly scoped #15085
Comments
It's been suggested that there is another workaround: julia> module X
macro p(y)
esc(quote
println($y)
end)
end
end Though I'd still argue that this is unintuitive and contrary to most implementations of symbol hygiene. |
That isn't really a workaround; it's how it's supposed to be written at the moment. However see #10940. |
So, if I understand, there's not any good way to mix module defined values with arguments passed to a macro if the arguments might be symbols? i.e.: julia> module Foo
greeting = "Hello"
macro bar(thing)
esc(quote
println("$(greeting), $(thing)")
end)
end
end
Foo
julia> using Foo
julia> test = "World"
"World"
julia> @Foo.bar(test)
ERROR: UndefVarError: greeting not defined
julia> macroexpand(:(@Foo.bar(test)))
quote # none, line 5:
println("$(greeting), $(thing)")
end |
You can change scope of module Foo
greeting = "Hello"
macro bar(thing)
Expr(:call,:println,
Expr(:string,:greeting,",",esc(thing)))
end
end |
You can use
|
@JeffBezanson it did not work for me quote # In[1], line 5:
Foo.println("$(Foo.greeting), $(Foo.esc(Foo.thing))")
end At first I thougt string interpolation is the problem but I slightly changed it to see julia> module Foo
greeting = "Hello"
macro bar(thing)
quote
println("$(greeting), $(esc(thing))")
esc(thing)
end
end
end With quote # In[13], line 5:
Foo.println("$(Foo.greeting), $(Foo.esc(Foo.thing))") # In[13], line 6:
Foo.esc(Foo.thing)
end This is on julia> versioninfo()
Julia Version 0.5.0-dev+2600
Commit 746fa80* (2016-02-12 07:38 UTC)
Platform Info:
System: Linux (arm-linux-gnueabihf)
CPU: ARMv7 Processor rev 0 (v7l)
WORD_SIZE: 32
BLAS: libopenblas (NO_AFFINITY ARMV7)
LAPACK: libopenblas
LIBM: libm
LLVM: libLLVM-3.7.1 |
String interpolation is actually the problem here. You need two
In this case, calling If you just put |
Typical usage looks like this:
|
Ok. I got what you mean and indeed it needs syntax interpolation in |
So, out of curiosity, what's the reason every argument to a macro is not Alternatively, I think one might reasonably ask why namespace qualification is part of the hygiene pass? For example, in Clojure the equivalent qualification is handled by the quasi-quote:
|
@mauro3 Ah, ok...I just went back and re-read that original thread instead. It seems like that issue (and the follow-up in #10940) covers everything, and whatever is not covered is probably better addressed in that thread anyway. Thanks all for the comments/help understanding Julia's macro/hygiene system! |
When a macro is imported from a foreign module, if a symbol is passed as an argument to that macro and used in an escape clause within a quote clause, the symbol has the macro-source-module name prefixed. This does not seem like desirable behavior.
Example:
Currently, this can be avoided by a bit of quote-escape-gymnastics:
This is currently causing bugs and other issues for at least a few users as discussed here and here.
The text was updated successfully, but these errors were encountered: