Macro question

module Foo
x = 1
end

@eval Foo begin
    macro m(y) y end
    @m(x)
end
ERROR: LoadError: LoadError: UndefVarError: @m not defined

I thought this would define the @m macro in the Foo module, and then be able to access it. What is happening and how do I fix it?

If you just do:

module Foo
x = 1
end

@eval Foo begin
    macro m(y) y end
end

Then it works (Foo.@m is defined). And you can:

julia> @eval Foo begin
           @m x
       end
1

I am not entirely sure what the problem is, but maybe it is a time-of-the-world problem.

2 Likes