O'Reilly logo

Metaprogramming Elixir by Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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__) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required