Help with macros and calling a macro from a different module

Does this explain it? Here is a function called in a macro, where only the function symbol is escaped (I left out all arguments for clarity):

macro test()
    quote
        $(esc(:dothis))()
    end
end

julia> @macroexpand @test()
quote
    dothis()
end

The resulting expression just calls dothis wherever it is used.

On the other hand, if we don’t escape the function symbol, it’s scoped to Main, where I defined the macro:

macro test2()
    quote
        $(:dothis)()
    end
end

julia> @macroexpand @test2()
quote
    Main.dothis()
end

The second version is a convoluted way of writing dothis() directly, but I hope it makes the contrast to the first version clearer.

3 Likes