MacroTools.jl icon indicating copy to clipboard operation
MacroTools.jl copied to clipboard

MacroTools not defined

Open cstjean opened this issue 8 years ago • 3 comments

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!

cstjean avatar Jan 09 '17 02:01 cstjean

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.

MikeInnes avatar Jan 09 '17 19:01 MikeInnes

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.

fingolfin avatar Dec 28 '21 01:12 fingolfin

My standard solution would be to macroexpand into $MacroTools.trymatch. Would that work here?

cstjean avatar Dec 28 '21 23:12 cstjean