Destructuring Declarations in Kotlin Android

What is Destructuring declaration?

It is term used in Kotlin programming language. Let’s assume we have a class named Data with field name, age, address. Sometimes we only need two of them in a particular code bloc so we can declare and initialize two variables at the same line. Yes you read it right, we can get value from Data class within single syntax. Let’s see it with example

data class Data(val name:String, val age:Int, val address:String)

 Initialize multiple variables from class member

val dataClass = Data("Lalit", 26, "Ahmedabad")
val (name, age, address) = dataClass

So, now we have three different variables. We can use name, age, address directly instead of You might think why we need this kind of declaration, but before thinking about it see below example.

Return two variables from function

This situation is very interesting or may be you also have faced similar problem where you need multiple values to be return from single function. This post is solution for it.

fun function(...): Data { 
        // computations return Data(name, age, address) 

// Now, to use this function:
val (name, age) = function(...)

Got the difference? Our function is capable to return Data class but for now we just want to display user’s name and age so, we can just initialize these two variables.

Ignore unused variables

Here when we are using Destructuring declaration we need to make sure sequence of the variables are same as the constructer of the class. Do we need to define all the variables of the class?. Answer is yes and no. Thank to little symbol “_” (Underscore). Kotlin uses that symbol to ignore variable. If we want to get name and address we can modify function call like below.

val (name, _,address) = function(...)

In this case two variables created name and address. Remember that we can only use it in the sequence of the declaration in constructor. Means we can only use (name, age, address) sequence in our example, which we have declared in constructor. We can ignore trailing variables but can not middle and starting.


This is all about Destructuring declaration in kotlin, In Android you can use this in DiffUtil class where comparing arethedatasame method.

Share this content:


More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message