Kotlin Academy · Lesson

Destructuring Declarations with componentN

Destructure data class instances into variables using component functions.

Lesson 3 of 413 steps

Destructuring Declarations with componentN is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

Destructuring Basics

A destructuring declaration unpacks an object into multiple variables in one step. It uses the componentN() operator functions.

Destructuring a Data Class

Data classes auto-generate component1, component2, ... so you can write val (a, b) = obj.

data class Point(val x: Int, val y: Int)
fun main() {
    val p = Point(3, 5)
    val (x, y) = p
    println("x=$x, y=$y")
}

Destructuring a Pair

The stdlib types Pair and Triple support destructuring out of the box.

fun main() {
    val pair = "Ada" to 35
    val (name, age) = pair
    println("$name is $age")
}

Destructuring a Triple

Triple holds three values; destructure into three variables.

fun main() {
    val rgb = Triple(255, 128, 0)
    val (r, g, b) = rgb
    println("R=$r G=$g B=$b")
}

Destructuring in Map Iteration

Map entries have component1 (key) and component2 (value). Destructure in a for loop header.

fun main() {
    val ages = mapOf("Alice" to 30, "Bob" to 25)
    for ((name, age) in ages) {
        println("$name -> $age")
    }
}

Destructuring in Lambdas

Lambdas accepting pairs can destructure parameters directly.

fun main() {
    val pairs = listOf("a" to 1, "b" to 2, "c" to 3)
    pairs.forEach { (letter, num) -> println("$letter=$num") }
}

Ignoring Values with _

Use underscore _ to skip values you don't need.

data class User(val name: String, val email: String, val age: Int)
fun main() {
    val u = User("Ada", "ada@example.com", 35)
    val (name, _, age) = u
    println("$name ($age)")
}

Returning Multiple Values

A function can return a Pair or a custom data class; the caller destructures.

data class Result(val sum: Int, val product: Int)
fun computeBoth(a: Int, b: Int) = Result(a + b, a * b)
fun main() {
    val (s, p) = computeBoth(4, 5)
    println("sum=$s product=$p")
}

Custom componentN

Any class can support destructuring by declaring operator fun componentN().

class RGB(val r: Int, val g: Int, val b: Int) {
    operator fun component1() = r
    operator fun component2() = g
    operator fun component3() = b
}
fun main() {
    val (red, green, blue) = RGB(255, 100, 0)
    println("$red $green $blue")
}

Destructuring with withIndex()

Pair index and value when iterating with withIndex().

fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    for ((idx, fruit) in fruits.withIndex()) {
        println("$idx: $fruit")
    }
}

Limitation: Position-Based

Destructuring is positional, not by name. Reordering data class fields silently changes meaning at call sites.

data class Point(val x: Int, val y: Int)
fun main() {
    val p = Point(10, 20)
    val (a, b) = p // a=x, b=y
    // Add a property in the middle and old destructuring shifts!
    println("$a $b")
}

Quick Check

Which operator function enables destructuring of the i-th property?

Recap

Destructuring uses componentN() operator functions. Data classes, Pair, Triple, and Map entries support it out of the box. Use _ to skip values, and beware: destructuring is positional, not name-based.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Destructuring Declarations with componentN” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Destructuring Declarations with componentN”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “Destructuring Declarations with componentN”?

Destructure data class instances into variables using component functions. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Destructuring Declarations with componentN” 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. Data Class Basics: Auto-generated Methods
  2. Copying with copy() and Partial Overrides
  3. Destructuring Declarations with componentN
  4. Data Classes in Collections and as Map Keys
← Back to Kotlin Academy