Skip to content

Syntax reference ​

Source: Elixir reference, Syntax reference.

The syntax of Elixir converts directly to 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). It is mostly uniform, with a few constructs of syntax sugar that reduce noise in common idioms. This page covers each construct and then its AST.

Reserved words ​

  • true, false, nil: 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
  • when, and, or, not, in: operators
  • fn: anonymous functions
  • do, end, catch, rescue, after, else: do-end blocks

Data types ​

Numbers ​

Digits, optionally separated by _ (1_000_000). Integers have no dot. Floats have a dot and at least one digit after it, and support scientific notation (123.4e10).

Atoms ​

  • Unquoted: a colon, then a Unicode letter or _, then letters, numbers, _, and @. It can end in ! or ?: :ok, :ISO8601, :integer?.
  • Quoted: :"…" or :'…' allows any Unicode character: :"++olá++", :"123". Quoted and unquoted atoms with the same name are equal (:atom, :"atom", :'atom'), but the compiler gives a warning about quotes that are not necessary.
  • All operators are valid atoms (:++). These are not valid: :@foo, :123, :(*).
  • true, false, and nil are the atoms :true, :false, and :nil.

Strings ​

Strings use double quotes and are UTF-8 binaries. Heredocs use """, end with a newline, and Elixir removes the indentation of the closing """ from every line. Strings are their own AST.

Charlists ​

Charlists are lists of code points. 'abc' === [97, 98, 99]. You write them in single quotes, with ''' for multiple lines. Both forms are deprecated in favor of the ~c SigilA custom textual representation. It starts with ~, then a letter and a delimiter, with optional modifiers after the last delimiter. You can define your own sigils.Read the page · Glossary. Charlists are their own AST.

Lists, tuples, binaries, maps, structs ​

[...], {...}, and <<...>> use commas as separators (a trailing comma is allowed). Maps are %{"hello" => 1}. Maps with atom keys and 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 have keyword notation: %{hello: "world"} is %{:hello => "world"} and [foo: :bar] is [{:foo, :bar}]. Structs put a name between % and {: %User{...}.

Expressions ​

  • Variables: they start with _ or a lowercase Unicode letter, then letters, numbers, and _. They can end in ? or !. By convention, they use snake_case.
  • Local calls: add(1, 2), with the rules for variables. Calls with no arguments need parentheses to prevent confusion with variables, and the parentheses must touch the name: add (1, 2) is a syntax error.
  • Operators: calls with precedence and associativity. =, when, &, and @ are operators. See Operators.
  • Remote calls: Math.add(1, 2), also for operators (Kernel.+(1, 2)) and quoted names (Math."++add++"(1, 2)). mod.fun() is a call, and map.field (no parentheses) accesses a field.
  • Aliases: they expand to atoms at compile time. See below.
  • Module attributes: the unary @ with a variable or local call: @foo "value" writes and @foo reads.
  • Blocks: expressions that newlines or ; separate. Parentheses create a block anywhere.

Aliases ​

An alias starts with an ASCII uppercase letter, then ASCII letters, numbers, or _ (no non-ASCII characters). It expands to an atom, and the dot is part of the name.

An alias is an atom with an Elixir. prefix.

The arrow -> ​

The arrow relates a left side (zero or more arguments) to a right side (zero or more expressions). It appears one or more times between do and end, fn and end, or ( and ). When it appears, only other clauses can be between those terminators.

Three places for clauses.

Sigils ​

A sigil has four parts. First comes ~. Then comes one lowercase letter or one or more uppercase letters. Then comes one of the pairs () {} [] <> "" '' || //. Last come optional modifiers (ASCII letters or digits). A sigil is a call to sigil_ plus the letter. The first argument is the contents as a string and the second argument is the modifiers as a list of integers. An uppercase letter disables interpolation: ~s/f#{"o"}o/ interpolates and ~S/f#{"o"}o/ does not.

The Elixir AST ​

The AST is a regular Elixir data structure. It holds atoms, integers, floats, strings, lists, 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 two elements, and tuples with three elements for calls and variables. A call sum(1, 2, 3) is {:sum, meta, [1, 2, 3]}: an atom (or another tuple), a keyword list of metadata (such as line numbers), and a list of arguments. quote shows it.

Source constructs and their AST.

Notes on the table:

  • A variable is {:sum, [], Elixir}: the third element is the context of the variable.
  • . is also an operator. A remote call has two arguments (the second is always an atom). A call to an anonymous function has one argument, which mirrors the "missing" name.
  • Aliases: all arguments after the first are atoms. __MODULE__.Bar.Baz puts a __MODULE__ call first.
  • Lists, and tuples with two elements, are literals: quote do: [1, 2, 3] is [1, 2, 3] and quote do: {1, 2} is {1, 2}.
  • Blocks are __block__ calls: quote do 1; 2; 3; end is {:__block__, [], [1, 2, 3]}.
  • -> is always inside a list. case 1 do 2 -> 3; 4 -> 5 end is {:case, [], [1, [do: [{:->, [], [[2], 3]}, {:->, [], [[4], 5]}]]]}. Between parentheses, it is a bare list of clauses, and fn wraps the clauses: {:fn, [], [{:->, [], [[1, 2], 3]}, …]}.
  • Qualified tuples (Foo.{Bar, Baz}, for multi-alias) are a :{} call on the dot, shown in the block below.
  • do-end blocks are keywords as the last argument, with the contents wrapped in parentheses: if true do this else that end is if(true, do: (this), else: (that)). Only after, catch, else, and rescue are allowed as extra block keywords (in receive, try, and similar). See Optional syntax.

This is the qualified tuple Foo.{Bar, Baz}:

elixir
quote do
  Foo.{Bar, Baz}
end
#=> {{:., [], [{:__aliases__, [], [:Foo]}, :{}]}, [],
#    [{:__aliases__, [], [:Bar]}, {:__aliases__, [], [:Baz]}]}

Site code: MIT. Pages and diagrams come from the Elixir documentation (Apache-2.0, snapshot of 2026-10-08, Elixir 1.20). This project is not affiliated with the Elixir Team.