Destructuring Declarations in Kotlin

Subscribe to my newsletter and never miss my upcoming articles

Kotlin is a brilliant multipurpose language that offers a wide array of awesome features to make lives easier for developers. Destructing is one of those things that you may find useful in your day-to-day development. So what is destructuring in Kotlin?

Simply put, destructuring refers to breaking down (destructure) an object into several variables. Assume that you have a class named Car that has three fields namely year, make, and colour. This data class will look like the following.

data class Car(val year: Int, val make: String, val colour: String)

With destructuring, you can assign the fields to a single variable and use them however way you wish. See the following code.

    val mercedes = Car(2020, "G-Class", "Macadamia Brown")

    val (year, make, colour) = mercedes

    println("The car of colour: $colour and make: $make was made in $year")

    //Will print
    The car of colour: Macadamia Brown and make: G-Class was made in 2020

Beneath the hood, Kotlin uses some methods to get the individual fields. The convention states that the name of these methods is component(). Mathematically, there would be N component() methods with N being the number of fields. In our case, these would be three component() methods as below:

val year = mercedes.component1()
val make = mercedes.component2()
val colour = mercedes.component3()

Applications of destructuring

Now that we have the basics down, let us explore how we can use this feature in our code. Imagine you have a list of cars that you want to iterate through and print their attributes. Normally, you would do the following.

    listOfCars.forEach { car ->
        println("This car is ${car.colour} of make ${car.make} and was made in ${car.year}")
    }

The above code will work just fine. However, considering what we know now, it is a bit bulky and can be improved by destructuring like below. Notice how the code is more concise now.

    listOfCars.forEach { (year, make, colour) ->
        println("This car is $colour of make $make and was made in $year")
    }

The above also works for traversing through a map as well as for loops.

It works for functions too!

Imagine you have a function that returns a car. For the sake of this piece, consider the following contrived example.

fun doNotCreateObjectsLikeThis(): Car {
    return Car(3030,"Why would you create cars like this?", "This is a contrived colour")
}

Since it returns a Car, you can use the result returned just like you would in the previous example. See the following code.

    val (yearContrived, makeContrived, colourContrived) = doNotCreateObjectsLikeThis()

    println("My contrived car is from $yearContrived of make $makeContrived and colour $colourContrived")

    //Will print
   My contrived car is from 3030 of make Why would you create cars like this? and colour This is a contrived colour

Final words

Hopefully, this article has given you a solid understanding of the cool feature that is destructuring in Kotlin. Use it to make your code more concise. If you wish, read more on them here.

In the meantime, let us connect on GitHub , LinkedIn, and Twitter.

Thank you and Happy Coding!

Bright Okwu's photo

Cool

Ferdinand Bada's photo

Glad you think so!

Edidiong Asikpo's photo

This is an amazing article. Thanks for sharing Ferdinand Bada.

Ferdinand Bada's photo

Anytime! Glad you enjoyed it!