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)
run
instead oflet
, you can drop theit
s. – Liselisettafor (i in 1..10) { a = i; b = -i }
– CarleecarleenPair
object into two existing variables" – Carleecarleen