TL;DR
In the previous post, we discussed Elixir’s short circuit operators and how those could help you to write less code. Today we explain what Elixir Tuples are and when to use them. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
Tuple Syntax
In the image above you can see how we defined test_case
Tuple. Tuple can have more than two elements. You get Tuple values using Kernel.elem
function, and update it using. Kernel.put_elem
As Elixir is immutable, test_case
is not changed, but a new variable is created in memory.
When To Use Tuples
We use tuples when we need to return from function simple data type. For example, to indicate that function returned an error with an error message.
Remember
- What is Tuple syntax
- How to get Tuple value
- How to set Tuple value
- When to use Tuples