Skip to content

Set-theoretic types cheatsheet ​

Source: Elixir cheatsheet, Set-theoretic types.

A type is a set of values. The type language has set operators to combine those sets.

Set operators ​

Types combine like sets: or, and, and not.
OperatorSyntaxMeaning
Uniontype1 or type2values in either
Intersectiontype1 and type2values in both
Differencetype1 and not type2values in the first but not the second
Negationnot typeevery value that is not in the type

Data types ​

The broad types and how they nest.

Broad types ​

bitstring(), binary(), empty_list(), integer(), float(), pid(), port(), reference().

binary() is a subtype of bitstring().

Atoms ​

TypeMatches
atom()all atoms
:ok, :error, SomeModuleone individual atom each

Functions ​

TypeMeaning
function()all functions
(-> :ok)a zero-arity function returning :ok
(integer() -> boolean())one argument
(binary(), binary() -> binary())two arguments
(integer() -> binary()) and (binary() -> atom())multiple clauses

An intersection of function types describes one function with several clauses:

Each clause applies to its own argument type.

Maps ​

TypeMeaning
map()all maps
empty_map()the empty map

Maps with atom keys

elixir
# Only has the keys name and age
%{name: binary(), age: integer()}

# Has the name key and age is optional
%{name: binary(), age: if_set(integer())}

# Has the keys name and age and may have other keys (open map)
%{..., name: binary(), age: integer()}

# Has the key name, may have other keys, but age is not set
%{..., name: binary(), age: not_set()}
Which values each map type accepts.

Maps with domain keys (always treated as optional)

elixir
# Has atom and binary keys
%{atom() => binary(), binary() => binary()}

# ...and may have other keys (open map)
%{..., atom() => binary(), binary() => binary()}

Maps with mixed keys

elixir
# atom keys with binary values, but a :root key of type integer
%{atom() => binary(), root: integer()}

# ...and may have other keys
%{..., atom() => binary(), root: integer()}

The domain key types are atom(), binary(), integer(), float(), fun(), list(), map(), pid(), port(), reference() and tuple().

Non-empty lists ​

TypeMeaning
non_empty_list(elem_type)a proper list
non_empty_list(elem_type, tail_type)an improper list, as long as tail_type does not include lists
A proper list ends in []. An improper list ends in something else.

Tuples ​

TypeMeaning
tuple()all tuples
{:ok, binary()}, {:error, binary(), term()}, {pid(), reference()}tuples of exactly n elements
{binary(), binary(), ...}tuples of at least n elements

Additional types for convenience ​

AliasDefinition
boolean()true or false
number()integer() or float()
list()empty_list() or non_empty_list(term())
list(a)empty_list() or non_empty_list(a)
list(a, b)empty_list() or non_empty_list(a, b)

Site code: MIT. Pages and diagrams are derived from the Elixir documentation (Apache-2.0). Not affiliated with the Elixir Team.