MacroTools.jl
MacroTools.jl copied to clipboard
MacroTools not defined
module MM
using MacroTools: @capture
@capture(:(x=2), _lhs = _rhs)
end
> UndefVarError: MacroTools not defined
It works if I import MacroTools. In my code, I usually fix this by writing function calls like $MacroTools.foo(...), but I don't use hygiene so I'm not sure if it's applicable in your case.
This is still definitely one of the most useful Julia packages for me, thank you!
Glad it's useful, thanks!
You can fix this easily enough by adding using MacroTools, but it's pretty surprising that the above syntax doesn't import the MacroTools name into scope itself (breaking the macro). I'd open an issue open Base Julia for this since it will affect all macros.
The code expanded by the macro invokes MacroTools.trymatch, which indeed is not available in the current scope after just doing using MacroTools: @capture. And I think this makes sense given that the expression returned by the macro is evaluated in the context of the calling module, not in the context of MacroTools. So I don't think there is a bug in Julia to report here?
If one was willing to require Julia 1.6, one could perhaps use something like import MacroTools as some_generated_symbol and then some_generated_symbol.trymatch.
My standard solution would be to macroexpand into $MacroTools.trymatch. Would that work here?