Macros: The Building Blocks of Elixir
It’s time to get our hands dirty and see what macros are all about. You’ve been promised custom language features, so let’s start small by re-creating an essential Elixir feature. From there, we’ll expose a few fundamental macro features and see how the AST ties in.
Re-Creating Elixir’s unless Macro
Let’s pretend for a moment that Elixir lacks a built-in unless construct. In most languages, we would have to settle for if ! expressions and learn to accept this syntactic shortcoming.
Fortunately for us, Elixir isn’t like most languages. Let’s define our own unless macro, using if as a building block of our implementation. Macros must be defined within modules, so let’s define a ControlFlow module. Head back ...
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.