October 2014
Intermediate to advanced
280 pages
7h 20m
English
The types we’ve seen so far are common in other programming languages. Now we’re getting into types that are more exotic, so we’ll go into more detail here.
Elixir collections can hold values of any type (including other collections).
A tuple is an ordered collection of values. As with all Elixir data structures, once created a tuple cannot be modified.
You write a tuple between braces, separating the elements with commas.
| | { 1, 2 } { :ok, 42, "next" } { :error, :enoent } |
A typical Elixir tuple has two to four elements—any more and you’ll probably want to look at maps,, or structs,.
You can use tuples in pattern matching:
| | iex> {status, count, action} = {:ok, 42, "next"} |
| | {:ok, 42, "next"} |
| | iex> status |
| |
Read now
Unlock full access