0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Deconstruction

Extract from data.

What Is Deconstruction?

Deconstruction (also called destructuring) means extracting the parts of a data structure directly in a pattern.

Instead of calling accessor methods, you describe the shape and name the pieces you want.

Deconstructing a Tuple

A tuple groups several values. You can pull them apart in a single binding by writing names in parentheses.

object Main {
  def main(args: Array[String]): Unit = {
    val pair = ("Ada", 1815)
    val (name, year) = pair
    println(s"$name was born in $year")
  }
}

All lessons in this course

  1. match Expressions
  2. Matching Types and Values
  3. Guards and Binding
  4. Deconstruction
← Back to Scala for Backend Engineering & Functional Programming