Destructing declaration

Kotlin is loaded with features. Some of the features are beyond our imagination. What if we tell you a function can return multiple values! Or you can catch the objects into multiple variables. Destructing declaration allows you to perform such things.

Let's say we need to return two things from a function. For example, a result object and a status of some sort. A compact way of doing this in Kotlin is to declare a data class (https://kotlinlang.org/docs/reference/data-classes.html) and return its instance.

In Kotlin, we can achieve this by writing the following code:

    data class Result(val result: Int, val status: Status)    fun function(...): Result {      // computations      return Result(result, status)    } // Now, to use this ...

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.