Scala Tuples – Syntax, Declaration, Use case, Examples

Introduction to Scala Tuples

Scala Logo - from Wikipedia
Scala Logo – from Wikipedia

After you have learnt about Scala Variables and Methods, you can now learn about Scala Tuples.

What is a Scala Tuple?

Tuple is kind of a weird term, the meaning in mathematics and in computer science is quite different and in different computer language it can mean different things as well. If you want to read all about it, feel free to check the article about tuple on Wikipedia.

A tuple is just a grouping of values. In Scala, a tuple is a more complex Type which is made out of a combination of several Types. Actually, a Type is a Tuple1, a tuple of one element.

For instance you could have a Tuple2 made of a String and an Int: (String, Int) . And a single Int would just be a Tuple1 made of an Int: (Int).

How to declare a Scala Tuple

Now that we know what it is, how do we use it ?

It is super easy, you have to use ( …,… ) syntax.

val myTuple: (Int, String) = ???

This syntax will declare a value of type (Int, String). This means that myTuple contains two parts, a first one which is an Int and a second part which is a String.

To set the value, you have to do it with the () as well.

val myValue: (String, String) = ( "This is part1" , "This is part2" )

As you can see, the ( ) are enclosing the declaration of the values.

Tuple are very powerful in Scala because you can use the compiler to help you.

val otherExample: (Double, String) = ("this is not a Double", "this is a String")

This line will fail to compile because the first element should be a Double but is assigned a String.

How to access the value of a Scala Tuple?

Now that you have a value containing a tuple how do you access its value ?

To access the value inside a Scala Tuple, you have to use the _N syntax. More specifically, you have to use _1 to access the first element, _2 to access the second one, etc…

val myTuple: (Int, String) = (12, "a string")

val twelve: Int = myTuple._1
val aString: String = myTuple._2

Those are read only, you cannot do:

val myTuple = (12, "a string")

myTuple._1 = 15 // No no 

Remember: Everything should be immutable in Scala.

Can I use it inside a Scala Method?

Of course you can !

def combine(
  firstTuple: (Double, String), 
  secondTuple: (Double, String)
): (Double, String) = {
    firstTuple._1 + secondTuple._1, 
    firstTuple._2 + secondTuple._2 

As you can see, we have two Scala tuples as input and an other tuple as an output.

Can I pass a Scala Tuple to a method?

Let’s say you have this method:

def sumAll(a: Int, b: Int, c: Int): Int = a + b + c

And you have an input data which looks like:

val input = (12, 5, 6)

You could do:

sumAll(input._1, input._2, input._3)

But we all agree that this is ugly. And if input was a method, you would end up computing the output three times.

Scala comes with a build-in method called tupled that every method can invoke.

Using this method allow you to do something much nicer:

(sumAll _).tupled(input)

The syntax might look weird but we will learn more about the details and why that works in a later article about the wildcard _.

For now, this feature must seem irrelevant, because instead of having input being a tuple you could just have it as three distinct values.

But you will see when we are going to talk about transformation and map that it can come in very handy.

Can I nest Scala Tuples?

Yes you can!

And it can get pretty crazy:

val input: (String, (String, Int, Double), (Int, (String, Double)))) = ???

is valid but it gets pretty crazy.

Accessing the values is looking pretty scary as well:

val foo: Double = input._3._2._2

To simplify this syntaxe we can use two tools provided by Scala: Pattern Matching and Case classes. We are going to talk about those in later articles as well.

An other option for the declaration part is to use type shortcut:

type myNewType = (Int, String)

val input: myNewType = (12, "Data")

val twelve = input._1

Reading the values look the same but at least the code will be a bit cleaner in the value and method declarations.

Practical example of Scala Tuples

Let’s go through a short example to practice what we just learnt.

Feel free to play with it.

If this does not work, you can directly go to Scastie.

What to remember?

A few things to remember about Scala Tuples:

  • ( and ) to declare a Scala Tuple
  • _1 to access the first element
    • _2 to access the second and so forth
  • Use (myMethod _).tupled(input) to use a method with a Scala Tuple when it was not originally written for.
  • You can nest tuples: (..., (..., ...) )
  • You can shorten your types and increase readability by declaring types: type myType = (String, Int)


You are now able to understand how to use tuples in Scala code.

Keep in mind that most of the time, you are going to use case class instead of Tuples.

Feel free to leave comments if you have any questions.

Leave a Reply

%d bloggers like this: