Junior — Middle
Can you explain what the component function componentN() does in your context?
sobes.tech AI
Answer from AI
In Kotlin, component functions componentN() are automatically generated for data classes and allow unpacking an object into individual variables using destructuring. For example, if you have a data class:
data class User(val name: String, val age: Int)
val user = User("Ivan", 30)
val (userName, userAge) = user // component1() returns name, component2() — age
Here, component1() returns the name, component2() — the age. This simplifies working with objects, making the code more readable and convenient.