(Duplicate) Kotlin doesn't allow destructuring with assignment [Closed]

I want to do swapping with making a Pair and destructuring deferred assignment like this.

var a = 2.0
var b = 1.0

(a, b) = b to a

But Kotlin doesn’t allow this syntax. We can only use destructuring when an immediate assignment.
Why we can’t use destructuring when assigning to existing variables?

1 Like

You should probably search the forum first, since this precise question was asked multiple times. But no, destructuring declarations are rather limited at the moment.

As for swap, it s discussed each time, but could you actually give an example, where you really need swap. It seems, that its usage is almost always a code smell.

1 Like

I’m sorry I didn’t find enough. I will Close this discussion.