Hello,
I’ve a module with a macro:
julia>
module MyModule
export @mymacro
macro mymacro(T)
return fieldnames( eval(T) )
end
end
When I’m at another module or at Julia’s shell I can’t use my macro:
julia> mutable struct Person
id::Int
name::String
end;
julia> using Main.MyModule
julia> @mymacro(Person)
ERROR: LoadError: UndefVarError: Person not defined
Stacktrace:
[1] top-level scope
@ :0
[2] eval
@ ./boot.jl:368 [inlined]
[3] eval(x::Symbol)
@ Main.MyModule ./REPL[1]:2
[4] var"@mymacro"(__source__::LineNumberNode, __module__::Module, T::Any)
@ Main.MyModule ./REPL[1]:6
in expression starting at REPL[4]:1
But if this macro is in the same module, like this:
julia> macro mymacrosamemodule(T)
return fieldnames( eval(T) )
end
julia> @mymacrosamemodule(Person)
(:id, :name)
The answer is what I want.
I think the problem is something about Julia Hygiene, and maybe the function esc()
could help me, but how to solve this problem?
Thanks