IntermediateAdvanced
Destructuring Declarations
Unpack data class properties into variables.
kotlin
data class User(val name: String, val age: Int)
fun main() {
val user = User("Amit", 24)
val (name, age) = user
println(name)
println(age)
}Output
Amit
24
Explanation
The (name, age) syntax unpacks the data class. Each variable receives the corresponding component.