Open In App
Related Articles

Destructuring Declarations in Kotlin

Improve Article
Save Article
Like Article

Kotlin provides the programmer with a unique way to work with instances of a class, in the form of destructuring declarations. A destructuring declaration is the one that creates and initializes multiple variables at once.
For example :

val (emp_id,salary) = employee

These multiple variables correspond to the properties of a class that are associated with an instance. These variables can be used independently in any way you want.

println(emp_id+" "+salary)

The destructing declaration works on the concept of component() functions. The number of variables that a destructing declaration can define, the class provide those number of component functions, starting from component1(), component2() up to componentN(). The data class in Kotlin by default implement component functions.

Destructuring declaration compiled to following code:-

val emp_id = employee.component1()
val salary = employee.component2()

Kotlin program of returning two values from a function –

// A sample data class
data class Data(val name:String,val age:Int)
// A function returning two values
fun sendData():Data{
    return Data("Jack",30)
fun main(){
    val obj = sendData()
    //  Using instance to access properties
    println("Name is ${}")
    println("Age is ${obj.age}")
    // Creating two variables using  destructing declaration
    val (name,age) = sendData()
    println("Name is $name")
    println("Age is $age")


Name is Jack
Age is 30
Name is Jack
Age is 30

Note: The component function definitions must be preceded by the operator keyword if to be used in a destructuring declaration.

Underscore for unused variables
Sometimes, you might want to ignore a variable in a destructuring declaration. To do so, put an underscore in place of its name. In this case, the component function for the given variable isn’t invoked.

Destructuring declaration in lambdas –

Following the arrival of Kotlin 1.1, the destructing declaration syntax can be used for lambda parameters also. If a lambda parameter has a parameter of Pair type or some other type that declares component functions, then we can introduce new parameters by putting them in parenthesis. The rules are the same as defined above.

Kotlin program of using destructuring declaration for lambda parameters –

fun main(){
    val map = mutableMapOf<Int,String>()
    println("Initial map is")
    // Destructuring a map entry into key and values
    val newmap = map.mapValues { (key,value) -> "Hello ${value}" }
    println("Map after appending Hello")


Initial map is
{1=Ishita, 2=Kamal, 3=Kanika, 4=Minal, 5=Neha, 6=Pratyush, 7=Shagun, 8=Shashank, 9=Uday, 10=Vandit}
Map after appending Hello
{1=Hello Ishita, 2=Hello Kamal, 3=Hello Kanika, 4=Hello Minal, 5=Hello Neha, 6=Hello Pratyush, 7=Hello Shagun, 8=Hello Shashank, 9=Hello Uday, 10=Hello Vandit}

If a component of the destructured parameter is not in use, we can replace it with the underscore to avoid calling component function:

map.mapValues { (_,value) -> "${value}" }

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Aug, 2019
Like Article
Save Article
Similar Reads