August 2017
Intermediate to advanced
440 pages
10h
English
Sometimes it makes sense to restructure objects into multiple variables. This syntax is called a destructuring declaration:
data class Person(val firstName: String, val lastName: String, val height: Int)
val person = Person("Igor", "Wojda", 180)
var (firstName, lastName, height) = person
println(firstName) // prints: "Igor"
println(lastName) // prints: "Wojda"
println(height) // prints: 180
A destructuring declaration allows us to create multiple variables at once. The preceding code will create values of the firstName, lastName, and height variables. Under the hood, the compiler will generate code like this:
val person = Person("Igor", "Wojda", 180) var firstName = person.component1() var lastName = person.component2() ...Read now
Unlock full access