Collection Types

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).

Tuples

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

Get Programming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.