Manipulating AST before compilation?

There are some compile-time changes to the AST I’d like to make. It seems __before_compile__/1 doesn’t allow for me to manipulate the existing AST of a module prior to compilation. I can append with the results of the __before_compile__ function.

Similarly, __after_compile__/2 seems too late in the pipeline.

Is there a way to do this?

1 Like

The way to do so is with a macro. Elixir was intentionally designed so you can’t rewrite the AST implicitly. You can provide a different type of def, like Nx’ defn, but you can’t simply change code.

2 Likes

That’s what I suspected, makes sense. Thanks