Junior — Middle
Is it possible to declare a variable val (first, second) using a pair object, where pair is a class Pair<String, String>?
sobes.tech AI
Answer from AI
In Kotlin (the main language for Android), you can declare a variable with object destructuring if the class supports componentN functions.
The class Pair<String, String> already implements these functions, so you can write:
val pair = Pair("firstValue", "secondValue")
val (first, second) = pair
println(first) // firstValue
println(second) // secondValue
Thus, declaring val (first, second) = pair is possible and correct if pair is an object of class Pair<String, String>. This is a convenient way to access the elements of the pair immediately.