Use macro globally

Hello.
I’d like to use macro in a phoenix project. I have to write ‘use Macro’ in each .ex file for applying the macro to the project, and it takes much time.

So I looking for a way to use macro globally in the phoenix project. Do you know of it?

You need to call the macro, wherever you want it to happen.

If you have something you already use everywhere and that you can change, then you can add the additional use to it’s returned AST.

4 Likes

You have some generated Phoenix modules with __using__ functions already inside so you have to do e.g. use MyAppWeb, :controller in your controllers and then just add the code you want to inject there.

Or you can make something entirely different in the yourapp_web.ex file and just call it.

So there’s no way to actually have something automatically injected (or if there’s a way that’s a severe malpractice anyway!) but you can piggyback on those that you are already using.

1 Like