0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Destructuring Tuples

Pull values back out.

Getting Values Out

Creating a tuple is only half the story. To use the values inside, you need to take them back out.

Scala gives you two main ways: numbered accessors and pattern destructuring. This lesson covers both.

Accessing by Position

Every tuple element has a position you can read with ._1, ._2, and so on.

Note that positions start at one, not zero. So ._1 is the first element.

val person = ("Alice", 30)
println(person._1)
println(person._2)

All lessons in this course

  1. Creating Tuples
  2. Destructuring Tuples
  3. Building Ranges
  4. Ranges in Loops
← Back to Scala for Backend Engineering & Functional Programming