July 2018
Intermediate to advanced
242 pages
8h 6m
English
It is often practical to convert a single object of a complex type into a number of variables. This allows you to provide proper naming for the variables and simplifies the code. Kotlin provides an easy, built-in way to achieve this with a feature called destructuring:
data class User(val login: String, val email: String, val birthday: LocalDate)fun getUser() = User("Agata", "ag@t.pl", LocalDate.of(1990, 1, 18))val (name, mail, birthday) = getUser()print("$name was born on $birthday")
As a result, this piece of code would print the following message to the console:
Agata was born on 1990-01-18
Pretty awesome! Destructuring is available for data classes out of the box. The Kotlin standard library provides this feature ...
Read now
Unlock full access