Compile-Time Hooks

Elixir allows us to set a special module attribute, @before_compile, to notify the compiler that an extra step is required just before compilation is finished. The @before_compile attribute accepts a module argument where a __before_compile__/1 macro must be defined. This macro is invoked just before compilation in order to perform a final bit of code generation. Let’s apply this hook to fix our test macro. Update your Assertion module with these @before_compile hooks:

macros/before_compile.exs
Line 1 
defmodule​ Assertion ​do
defmacro​ __using__(_options) ​do
quote​ ​do
import​ ​unquote​(__MODULE__)
Module.register_attribute __MODULE__, :tests, accumulate: ​true
@before_compile​ ​unquote​(__MODULE__) ...

Get Metaprogramming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.