Destructuring Declarations in Kotlin

Sometimes it’s easy to destructure an object into multiple variables of different properties instead of calling it again and again using . operator on the property or instance itself. It is beneficial for a few reasons like,

  1. We don’t have to use the dot property on the object again and again.
  2. If the object is of a nullable type, we can do the null check only once and then we can destructure it to non-nullable properties.
  3. The defined variable will be alive within the defined scope.
  4. Fewer chances of confusion and more readability.
This post was originally posted at https://agrawalsuneet.github.io/blogs/destructuring-declarations-in-kotlin/ and reposted on Medium on 03rd Dec 2021.

What is destructuring declaration?

Let’s try to understand what is a destructuring declaration and how does it works internally.

Usually, when we declare a property, we can initialise it with some value or mark it nullable. If we want to extract a property from an object, we need to do that for each and every property, individually.

Kotlin provides functionality to do that in a single declaration where we can destructure the object into multiple variables within a single declaration.

data class Employee (val name : String,
val age: Int,
val department: String)
var employee = Employee(name = "Suneet", age = 29, department = "Engineering")

var (name, age, department) = employee

println("$name at the age of $age is working in $department department")

The output for the above print statement would be

Suneet at the age of 29 is working in Engineering department

How does it works internally?

The compiler internally uses the componentN function of Kotlin to destructure the object and assign them to the properties individually.

componentN is a functionality provided by Kotlin for any data class which return the Nth property (in sequence from the beginning) of that data class.

var (name, age, department) = employee// is equivalent tovar name = employee.component1()
var age = employee.component2()
var department = employee.component3()

Please continue reading at https://agrawalsuneet.github.io/blogs/destructuring-declarations-in-kotlin/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ebakus aims to improve the user experience of dApps and make them more accessible.

Research Project Management Tool Development using MERN Stack (Backend)

Callbacks from webView to JavaScript: sharing data between native code and html

Introducing Luna — JavaScript Testing Done Right

NodeJs/Mocha Test Case for AWS SDK APIs

What I Learned at Work this Week: Preventing Errors

Creating a Backblaze Database in Node.js/Javascript

Unity — Using TextMesh to render a text on sprite

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

Introduction to Kotlin Functions

Playing with Kotlin Type-safe builders

Diving deep into Kotlin Coroutines.

Advent of Code 2021 in Kotlin