Destructuring
Structuring, or construction, is creating an object from values in different variables. Destructuring is the opposite—to extract values into variables from within an existing object. This facility is useful to remove noisy, repetitive code. Kotlin has the destructuring capability much like in languages such as JavaScript. But unlike JavaScript, the destructuring in Kotlin is based on the position of properties instead of the names of the properties.
Let’s start with a piece of code that is verbose and then refactor that code, using destructuring, to make it concise. Triple is a class in the Kotlin standard library that represents a tuple. We’ll look at it further in Using Pair and Triple. For now, we’ll use Triple to return ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access