Multiple assignment in Kotlin

Viewed 170

I can declare and initialize a and b like this

var (a, b) = Pair(1, 2)

But I can't reassign them like this

(a, b) = Pair(3, 4)

How come this isn't possible? Am I missing something?

2 Answers

that because (a, b) is 2 separate variables, that you initialize by componnetN() function.
you can use a and b as different variables. If you vant have pair you have to write it like this var a = Pair(2, 1) or can use var a = 1 to 2

read here for more ingormation
https://kotlinlang.org/docs/destructuring-declarations.html

That's because in Kotlin (a, b, c, ...) is not a groupping operator but a destructuring declaration and hence it only works for declaring variables, not for reassigning.

Related