Appearance
Quote and unquote
Read first: Lists and tuples
Source: Elixir guide, Quote and unquote.
Meta-programming rests on one idea: the data structures of Elixir can represent an Elixir program. This chapter looks at those structures and at quote/2 and unquote/1. Macros and DSLs build on them.
Quoting
The building block of a program is a TupleA fixed collection with curly brackets that can hold any value. Its elements are stored one after the other in memory, so it is fast to read one element by index or to get the size.Read the page · Glossary with three elements. sum(1, 2, 3) is:
elixir
quote do: sum(1, 2, 3)
#=> {:sum, [], [1, 2, 3]}Operators are also calls: quote do: 1 + 2 is {:+, [context: Elixir, import: Kernel], [1, 2]}. A map is a call to %{}: {:%{}, [], [{1, 2}]}. A variable has an AtomA constant with a value that is its own name, such as :ok. Two atoms are equal if their names are equal.Read the page · Glossary, not a list, in the third position: quote do: x is {:x, [], Elixir}.
Nested code makes a tree. Other languages call it an ASTAbstract Syntax Tree. Elixir represents code as nested tuples in the shape of a tree, and calls them quoted expressions.Read the page · Glossary (AST). Elixir calls it a Quoted expressionThe Elixir name for the AST: code as nested tuples. quote produces it and unquote inserts values into it.Read the page · Glossary:
elixir
quote do: sum(1, 2 + 3, 4)
#=> {:sum, [], [1, {:+, [context: Elixir, import: Kernel], [2, 3]}, 4]}Macro.to_string/1 turns a quoted expression back into text: "sum(1, 2 + 3, 4)".
The general shape is {atom | tuple, list, list | atom}:
- The first element is an atom, or another tuple in the same shape.
- The second element is a Keyword listA list of tuples with two elements, where the first element, the key, is an atom. The keys keep the order that you gave and can repeat. People mostly use them for optional arguments to functions.Read the page · Glossary of metadata (line numbers and contexts).
- The third element is the list of arguments, or an atom if the tuple is a variable.
Five literals quote to themselves: atoms, numbers, lists, strings, and tuples with two elements.
Under the hood Our own text. It is not from the official docs.
A Quoted expressionThe Elixir name for the AST: code as nested tuples. quote produces it and unquote inserts values into it.Read the page · Glossary is ordinary data on the heap. quote do: sum(1, 2, 3) is the TupleA fixed collection with curly brackets that can hold any value. Its elements are stored one after the other in memory, so it is fast to read one element by index or to get the size.Read the page · Glossary {:sum, [], [1, 2, 3]}. It uses 4 words for the tuple and 6 words for the list of arguments, 10 words in all (:erts_debug.flat_size/1). A nested call adds its own tuple and metadata: sum(1, 2 + 3, 4) took 38 words. The + carries [context: Elixir, import: Kernel]. A function of three lines is 28 bytes as source text. After the parser reads it with its line information, it is 640 bytes of heap. Nothing here is special to the VM. The compiler walks these tuples and lists as it walks any others, and MacroCode that receives quoted expressions and changes them into something else. Macros are harder to write than functions, and it is bad style to use them when you do not need them.Read the page · Glossary receive and return them.
Sources: we measured on Elixir 1.20 / OTPOpen Telecom Platform: the set of libraries in Erlang that you use to build fault-tolerant applications. It includes supervision trees and event managers.Read the page · Glossary 29. The sizes depend on the metadata that the parser adds.
Unquoting
quote gives you the code as you wrote it. To inject a value into it, use unquote/1:
elixir
number = 13
Macro.to_string(quote do: 11 + number) #=> "11 + number"
Macro.to_string(quote do: 11 + unquote(number)) #=> "11 + 13"unquote/1 can also inject a function name: quote do: unquote(fun)(:world) with fun = :hello gives "hello(:world)".
To inject many values into a list, use unquote_splicing/1. A plain unquote nests the list:
elixir
inner = [3, 4, 5]
Macro.to_string(quote do: [1, 2, unquote(inner), 6]) #=> "[1, 2, [3, 4, 5], 6]"
Macro.to_string(quote do: [1, 2, unquote_splicing(inner), 6]) #=> "[1, 2, 3, 4, 5, 6]"A MacroCode that receives quoted expressions and changes them into something else. Macros are harder to write than functions, and it is bad style to use them when you do not need them.Read the page · Glossary receives chunks of code and injects them into other chunks. This is how code transformation and code generation work at compile time.
Escaping
Not every value is a valid quoted expression. A map is not, and a tuple with four elements is not. You can express such a value as a quoted expression. To inject it into quoted code, escape it first:
elixir
Macro.escape(%{hello: :world})
#=> {:%{}, [], [hello: :world]}Keep the two apart: a regular value (a list, a map, a process, a reference) and a quoted expression. Integers, atoms, and strings are their own quoted form. You must convert maps. You cannot convert functions and references at all. The Macro module has many functions to work with the AST.