Destructing
Learn how to use position-based destructuring in Kotlin to assign multiple variables to components of a single object and explore its advantages and pitfalls.
Exploring position-based destructuring
Kotlin supports a feature called position-based destructuring, which lets us assign multiple variables to components of a single object. For that, we place our variable names in parentheses.
Press + to interact
data class Player(val id: Int,val name: String,val points: Int)fun main() {val player = Player(0, "Gecko", 9999)val (id, name, pts) = playerprintln(id) // 0println(name) // Geckoprintln(pts) // 9999}
Destructuring mechanism
This mechanism ...