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

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

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