O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required