Appearance
Set-theoretic types cheatsheet
Read first: Gradual set-theoretic types
Source: Elixir cheatsheet, Set-theoretic types.
A type is a set of values. The type language has set operators that combine those sets.
Set operators
| Operator | Syntax | Meaning |
|---|---|---|
| Union | type1 or type2 | values in either type |
| Intersection | type1 and type2 | values in both types |
| Difference | type1 and not type2 | values in the first type but not in the second type |
| Negation | not type | every value that is not in the type |
Data types
Broad types
bitstring(), binary(), empty_list(), integer(), float(), pid(), port(), reference().
binary() is a subtype of bitstring().
Atoms
| Type | Matches |
|---|---|
atom() | all 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 |
:ok, :error, SomeModule | one atom each |
Functions
| Type | Meaning |
|---|---|
function() | all functions |
(-> :ok) | a function with no arguments that returns :ok |
(integer() -> boolean()) | a function with one argument |
(binary(), binary() -> binary()) | a function with two arguments |
(integer() -> binary()) and (binary() -> atom()) | a function with multiple clauses |
An intersection of function types describes one function with several clauses:
Maps
| Type | Meaning |
|---|---|
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()}Maps with domain keys (always 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
| Type | Meaning |
|---|---|
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 |
Tuples
| Type | Meaning |
|---|---|
tuple() | all 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 |
{:ok, binary()}, {:error, binary(), term()}, {pid(), reference()} | tuples of exactly n elements |
{binary(), binary(), ...} | tuples of at least n elements |
Extra types for convenience
| Alias | Definition |
|---|---|
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) |