Skip to main content
A destructuring declaration is a syntax mechanism in Kotlin that unpacks a single composite object into multiple distinct variables simultaneously. It operates by invoking positional componentN() functions defined on the source object, mapping each extracted value to a corresponding variable in the declaration.

Compiler Translation

Destructuring is a convention-based feature. When the Kotlin compiler encounters a destructuring declaration, it translates the syntax into sequential calls to component1(), component2(), and so forth.

The componentN() Operator

For an object to be destructurable, its class must provide componentN() functions. These functions are subject to strict compiler requirements:
  1. They must be prefixed with the operator modifier.
  2. They must be callable without arguments.
  3. The return type of the componentN() function dictates the type of the resulting variable.
These functions can be defined either as member functions within the class or as extension functions.

Manual Implementation

To enable destructuring on a standard class, you must explicitly define the operator functions:

Extension Function Implementation

If you do not own the source code of a class, you can provide destructuring capabilities via extension functions. For example, adding destructuring to the standard Java File class:

Data Classes

When a class is declared with the data modifier, the Kotlin compiler automatically synthesizes the componentN() functions. These functions are generated exclusively for the properties defined in the primary constructor, strictly following their declaration order.
Note: Properties defined in the class body are excluded from the generated componentN() functions.

Destructuring in Control Flow and Lambdas

Destructuring declarations are natively supported in for loops and lambda expressions, provided the underlying collection elements or lambda parameters possess the requisite componentN() functions.

for Loops

When iterating over a collection, the loop variable can be destructured. This is commonly used with Map, as the Kotlin Standard Library provides component1() and component2() extensions for Map.Entry.

Lambdas

In higher-order functions, a lambda parameter can be destructured by wrapping the parameter names in parentheses.
If the lambda expects two distinct parameters rather than a single destructurable object, parentheses are omitted. The presence of parentheses explicitly instructs the compiler to invoke componentN() on a single incoming parameter.

Ignoring Components

If a specific component in the destructuring sequence is not needed, you can substitute the variable name with an underscore (_). This is a compiler-level optimization; the compiler will skip the generation of the corresponding componentN() call entirely, avoiding unnecessary allocations or computations.
In this example, only session.component2() is invoked. component1() and component3() are ignored.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More