I have a function that returns Pair:
fun createTuple(a: Int, b: Int): Pair<Int, Int> {
return Pair(a, b)
}
I want to initialize variables a and b using this function and then reassign them inside loop:
var (a, b) = createTuple(0, 0)
for (i in 1..10) {
createTuple(i, -i).let{
a = it.first
b = it.second
}
println("a=$a; b=$b")
}
Using let seems awkward. Is there a better way to unwrap Pair inside loop?
The following lines do not compile:
(a, b) = createTuple(i, -i)
a, b = createTuple(i, -i)
var (a, b) = createPair(0, 0) compiles fine for me.
Your problem probably is using createTuple(i, -i) instead of createPair(i, -i).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With