Trying to output a local import statement. The export statement works fine, but can’t figure out local import inside or outside quote. I tried running esc, QuoteNode, symbol… all sorts of combinations.
macro testmacro(T)
name = T
tname = Symbol(name, "Extra")
blk = quote
module $(esc(name))
$(esc(tname)) = "test"
end
import . $(esc(name)) : $(esc(tname)) # <- doesn't work
export $(esc(name)), $(esc(tname))
end
# push!(blk.args, :(import $(Symbol(".$(modname):")) $(typename))) # <- doesn't work
blk.head = :toplevel
return blk
end
I think it’s because of your extra spacing around the .
. This seems to work:
julia> mod = :MyModule
:MyModule
julia> f = :sin
:sin
julia> :(import .$mod: $f)
:(import .MyModule: sin)
Once in a while, I get stuck with this kind of problem. If I can’t write the expression with quote
, I just dump
what I want to produce, and build it “manually” with Expr
and such. It’s uglier, but it always works.
julia> dump(:(import .MyModule: sin))
Expr
head: Symbol import
args: Array{Any}((1,))
1: Expr
head: Symbol :
args: Array{Any}((2,))
1: Expr
head: Symbol .
args: Array{Any}((2,))
1: Symbol .
2: Symbol MyModule
2: Expr
head: Symbol .
args: Array{Any}((1,))
1: Symbol sin
As an aside, reading your code, I would just esc(quote ... end)
, instead of escaping individual elements…