Deconstruction in Kotlin
There are times when you need to extract an object into a number of variables.
Let’s take the example below:
|
|
In Kotlin, this is known as a destructuring declaration
. These new variables can now be used independently.
The compilation thereof would look something like this, but is not required:
Returning multiple values from a Function
Much like deconstruction of variables and collections, functions too can return multiple values.
|
|
Destructuring on Maps
The same can be done for a map
.
When to use an _ (underscore) in Destructuring
Underscores (_
) are used when you want to ignore the returned value in a specific position.
Let’s take the following example:
|
|
We will only have access to the name
variable response now, and not the id
.