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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.