What is a destructuring declaration in Kotlin, and what makes `val (a, b) = point` work under the hood?
answer
- Parentheses on the left = componentN() calls, not a tuple
- component1() starts at 1, positional
- data class auto-generates for primary-ctor props
- operator keyword required
- for ((k,v) in map) uses Map.Entry components
basics
~10 sIt lets you pull several values out of one object in a single line, like val (x, y) = point. Kotlin does it by calling special functions named component1(), component2(), and so on.
solid answer
~40 sA destructuring declaration unpacks an object into multiple variables at once: `val (x, y) = point`. The compiler rewrites it into separate `val` declarations that call positional operator functions: `val x = point.component1()` and `val y = point.component2()`. These `componentN()` functions are conventions marked with the `operator` keyword. `data class` generates them automatically for its primary-constructor properties, in declaration order. Order matters, not names — `val (lat, lng) = point` still binds component1() to `lat`. Any class (even non-data) can support destructuring by declaring `operator fun componentN()` manually. Common uses include iterating a `Map` (`for ((k, v) in map)`) and lambda parameters.
code
kotlin · 10 linesdata class Point(val x: Int, val y: Int)
fun main() {
val (x, y) = Point(3, 7)
println("$x, $y") // 3, 7
// compiler-equivalent:
val p = Point(3, 7)
val x2 = p.component1()
val y2 = p.component2()
}go deeper
Knows the syntax val (a, b) = obj and that data classes support it out of the box.
Explains the componentN() rewrite, the operator keyword, and that binding is positional.
Notes only primary-constructor props get generated components, and that any type can opt in manually.
Discusses positional-binding fragility as an API-design hazard and when to avoid exposing destructuring publicly.
## What it is A **destructuring declaration** assigns several variables from a single object in one statement: ```kotlin data class Point(val x: Int, val y: Int) val point = Point(3, 7) val (x, y) = point // x = 3, y = 7 ``` ## How the compiler expands it The parentheses are NOT a tuple. The compiler translates the declaration into one ordinary `val` per name, each calling a positional **operator function**: ```kotlin val x = point.component1() val y = point.component2() ``` These `componentN()` functions must be marked with the `operator` keyword and follow the naming convention `component1`, `component2`, … starting at 1. ## Where they come from - For a `data class`, the compiler **auto-generates** `componentN()` for each property declared in the **primary constructor**, in declaration order. Properties added in the class body do NOT get one. - For any other type you can write them by hand: ```kotlin class Pair2(val a: Int, val b: Int) { operator fun component1() = a operator fun component2() = b } ``` ## Position, not name Binding is purely positional. `val (lat, lng) = point` binds `lat` to `component1()` and `lng` to `component2()` regardless of the names. Choosing the wrong order silently gives wrong values — a real bug source. ## Common contexts - Maps: `for ((key, value) in map) { ... }` works because `Map.Entry` defines `component1()`/`component2()` as extensions. - Lambdas: `list.map { (a, b) -> a + b }` destructures each element parameter. - Multiple return: return a `data class` (or `Pair`/`Triple`) and destructure at the call site. Destructuring is a compile-time convention, costs nothing extra beyond the function calls, and is unrelated to pattern matching in other languages.
- Does destructuring match by variable name or by position?By position. The first name gets component1(), the second component2(), etc. Names are arbitrary; only order matters.
- Can a non-data class be destructured?Yes — declare `operator fun componentN()` manually. data class just generates them for you.
Like unpacking a labeled box by slot number, not by label name — slot 1 always goes to the first variable.
saying these in an interview costs you the question
- Thinking `val (a, b)` creates a tuple object
- Believing destructuring matches property names
- Claiming only data classes can ever be destructured
- Saying componentN() starts at 0