Metaprogramming in Elixir
In Elixir, metaprogramming is mostly used as a way to extend the language’s functionality and design DSL to make code more readable and digestible. However, unlike many other metaprogrammable languages, Elixir metaprogramming is quite restrictive. There are some safety mechanisms baked into Elixir’s core library to prevent developers from doing something that could easily break the language. For example, you cannot define a function in a module after it has already been defined. We will cover more such mechanisms later in this chapter.
Metaprogramming in Elixir revolves around three main pillars:
- Quoted literals: The Elixir representation of an Abstract Syntax Tree (AST) of an Elixir program
- Dynamic code injection: Injecting behavior into existing code dynamically
- Compile-time callbacks: Updating a module or the behavior of a function before or after its elements are defined
We will first start by understanding how an...