Destructuring Declarations

A destructuring declaration unpacks an object into several separate variables in a single statement, as in val (x, y) = point , pulling each component out by position.

Learn Destructuring Declarations in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

It shines with pairs, data classes, map entries, and lambda parameters, making code that handles grouped values dramatically cleaner.

What You'll Learn in This Lesson

1️⃣ Destructuring Pairs & Data Classes

The simplest case unpacks a Pair or Triple into named values. Behind the scenes Kotlin calls component1() , component2() , and so on — so anything that defines those operators can be destructured.

A data class automatically generates those componentN() functions, so its instances destructure too — handy for returning several values from a function:

2️⃣ Destructuring in for Loops over Maps

Iterating a Map is where destructuring pays off most. Each entry unpacks into (key, value) , so the loop reads like plain English instead of repeatedly calling .key and .value .

3️⃣ Skipping with _ and Lambdas

When a type has more components than you need, mark the ones to ignore with an underscore _ . You can also destructure right in a lambda's parameter list, which is perfect for collection operations.

Your turn. Fill in the ___ blanks, then run and compare.

Loop a list of coordinate pairs with destructuring and print each point's sum.

📋 Quick Reference — Destructuring

Practice quiz

What does 'val (a, b) = thing' translate into under the hood?

  • val a = thing.component1() and val b = thing.component2()
  • Two calls to toString()
  • A map lookup by name
  • A copy() call

Answer: val a = thing.component1() and val b = thing.component2(). Destructuring calls component1(), component2(), and so on by position.

Is destructuring positional or by name?

  • By the names you choose
  • Strictly positional
  • Alphabetical by property name
  • Random

Answer: Strictly positional. It is strictly positional; the first variable gets component1() regardless of its name.

How do you skip a component you don't need?

  • Use the keyword skip
  • Leave a blank space
  • Use null in that slot
  • Use an underscore _ in that position

Answer: Use an underscore _ in that position. An underscore _ discards the component in that position.

How does each Map entry destructure in a for loop?

  • Into (key, value)
  • Into (value, key)
  • Into a single Pair object only
  • It cannot be destructured

Answer: Into (key, value). for ((key, value) in map) unpacks each entry into key and value.

Which types support destructuring out of the box?

  • Only data classes
  • Only Pair
  • Pair, Triple, Map entries, and data classes
  • Every class automatically

Answer: Pair, Triple, Map entries, and data classes. Pair, Triple, Map entries, and data classes all provide componentN() automatically.

For 'data class User(val name: String, val age: Int)', what does 'val (age, name) = user' assign to your 'age' variable?

  • The user's name (component1)
  • The user's age
  • null
  • A compile error

Answer: The user's name (component1). Positional matching means your first variable gets component1(), which is name.

Can a regular (non-data) class with no componentN() be destructured?

  • Yes, always
  • No, not without componentN() operators
  • Only if it is open
  • Only inside a loop

Answer: No, not without componentN() operators. Without componentN() operators a plain class cannot be destructured.

What is the correct lambda syntax to destructure a Pair parameter?

  • forEach { name, score -> }
  • forEach { name; score -> }

Destructured lambda parameters need parentheses: { (name, score) -> }.

What does a data class auto-generate to enable destructuring?

  • componentN() functions
  • An iterator
  • A toString only
  • A compareTo only

Answer: componentN() functions. Data classes auto-generate component1(), component2(), and so on.

Where is destructuring especially idiomatic?

  • Only in constructors
  • Iterating a Map with for ((k, v) in map)
  • Only in test code
  • Inside string templates

Answer: Iterating a Map with for ((k, v) in map). Looping a Map with for ((k, v) in map) is a classic, readable use of destructuring.