The notation (x, y)
is a tuple of 2 elements, x
and y
. There are different ways to get access to the individual values in a tuple. You can use the ._1
, ._2
notation to get at the elements:
val tup = (3, "Hello") // A tuple with two elements
val number = tup._1 // Gets the first element (3) from the tuple
val text = tup._2 // Gets the second element ("Hello") from the tuple
You can also use pattern matching. One way to extract the two values is like this:
val (number, text) = tup
Unlike a collection (for example, a List
) a tuple has a fixed number of values (it's not always exactly two values) and the values can have different types (such as an Int
and a String
in the example above).
There are many tutorials about Scala tuples, for example: Scala tuple examples and syntax.