An Elixir program can be represented by its own data structures as an abstract syntax tree (AST) of tuples with three elements. For example, the function call
run(1, 2, 3) is represented internally as:
Macros in Elixir lets you perform syntactic extensions, which are expanded to before a program executes. We use macros to transform our internal program structure by treating code as data, and thus metaprogram.
Many of Elixir’s language features such as
def are in fact macros, which are translated away into lower-level abstractions during compilation. Armed with the same tools that José utilized to write Elixir’s standard libraries, you can extend Elixir to suit your needs using macros.
In the snippet above, we defined a macro with
defmacro, and take in a block of code as arguments. Macros in Elixir transforms the block of
code into its internal Abstract Syntax Tree (AST) representation, which is a nested tree of triples (3-size tuples.) When the macro
code, that internal representation is injected back into the global program’s compile tree. In the above example, we just return
What’s most useful here is that we can modify that internal representation before returning, transforming it into a completely different piece of code. We can have both performant and concise code by hiding away low-level optimizations within high-level macros.
codewas never executed/evaluated, only the returned
quote lets us transform a block of code into its internal AST representation. Internally,
defmacro calls this method to passed in parameters.
In the example above, we return a new internal representation
newcode instead of the original
code. Running the macro on any block of code results in the insertion and execution of our
What if we want to evaluate a code block?
unquote lets you defer execution of the code block it receives, only running it when the code generated by
quote is executed. You can only use
Here’s an example:
In the above example, we evaluate code using
unquote and use its result to perform some computation dispatched via pattern matching.
Macros lets you grow from a language consumer to a language creator. Elixir’s Open Language takes a step forward from Open Classes in Ruby. Armed with the same tools that José utilized to write Elixir’s standard libraries, you can extend Elixir to suit your needs, with features that are as first-class as
📬 Subscribe to my newsletter
Get notified of my latest articles by providing your email below.