0Pricing
Kotlin Academy · Lesson

Destructuring Declarations

Unpack into variables.

Destructuring Declarations is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Unpacking Objects

Sometimes you want to pull several properties out of an object at once.

Kotlin's destructuring declarations let you assign multiple variables from one object in a single line.

Basic Destructuring

Put names in parentheses on the left of =. Kotlin assigns the object's components to them in order.

Data classes support this automatically.

data class User(val name: String, val age: Int)

fun main() {
    val user = User("Alice", 30)
    val (name, age) = user
    println(name)
    println(age)
}

How It Works

Behind the scenes, destructuring calls generated component1(), component2(), and so on.

The position of each variable maps to the matching component function.

data class Point(val x: Int, val y: Int)

fun main() {
    val p = Point(3, 7)
    println(p.component1())
    println(p.component2())
}

Order Matters

Variables are matched by position, not by name.

The first variable always gets the first constructor property, regardless of what you call it.

data class Pair2(val first: Int, val second: Int)

fun main() {
    val (a, b) = Pair2(10, 20)
    println(a) // first property
    println(b) // second property
}

Skipping Components

Use an underscore _ to skip a component you do not need.

This keeps your code clean when only some values matter.

data class User(val name: String, val age: Int, val city: String)

fun main() {
    val (name, _, city) = User("Mia", 28, "Paris")
    println(name)
    println(city)
}

Destructuring in Loops

Destructuring shines when iterating over collections of objects.

You can unpack each element directly in the for header.

data class User(val name: String, val age: Int)

fun main() {
    val users = listOf(User("A", 20), User("B", 25))
    for ((name, age) in users) {
        println(name + " is " + age)
    }
}

Destructuring Map Entries

Maps support destructuring because their entries provide key and value components.

This makes iterating over a map very readable.

fun main() {
    val scores = mapOf("Alice" to 90, "Bob" to 85)
    for ((name, score) in scores) {
        println(name + ": " + score)
    }
}

Returning Multiple Values

A function can return a data class, and the caller can destructure the result.

This is a clean alternative to output parameters.

data class MinMax(val min: Int, val max: Int)

fun minMax(nums: List<Int>): MinMax {
    return MinMax(nums.min(), nums.max())
}

fun main() {
    val (low, high) = minMax(listOf(3, 1, 8, 5))
    println(low)
    println(high)
}

Destructuring Pair and Triple

The standard library Pair and Triple support destructuring too.

They are handy for quick groupings without defining a class.

fun main() {
    val pair = Pair("x", 1)
    val (label, value) = pair
    println(label)
    println(value)
}

Lambda Parameter Destructuring

You can destructure right inside a lambda's parameter list.

This is common when mapping over entries or pairs.

fun main() {
    val map = mapOf(1 to "one", 2 to "two")
    val text = map.map { (key, value) -> "" + key + "=" + value }
    println(text)
}

Putting It Together

Destructuring unpacks objects into variables:

  • Use parentheses on the left of =
  • Components are matched by position
  • Skip with _
  • Works in loops, lambdas, and returns
data class Result(val ok: Boolean, val message: String)

fun main() {
    val (ok, message) = Result(true, "done")
    println(ok)
    println(message)
}

Quick Check

Test your understanding of destructuring declarations.

Recap

You learned destructuring declarations:

  • They unpack an object into multiple variables
  • Matching is by position via componentN()
  • _ skips unwanted components
  • They work in loops, maps, lambdas, and returns

Next you will use data classes inside collections like sets and maps.

Frequently asked questions

Is the “Destructuring Declarations” lesson free?

Yes — the full text of “Destructuring Declarations” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Destructuring Declarations”?

Unpack into variables. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Destructuring Declarations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Data Classes
  2. copy and equals
  3. Destructuring Declarations
  4. Data Classes in Collections
← Back to Kotlin Academy