I'm looking at the description of the assignment operator in the Swift language reference. Is it guaranteed that destructuring assignment is made in parallel? As opposed to serial assignment. I don't see that point addressed in the description of the assignment operator.
Just to be clear, is it guaranteed that (a, b) = (b, a)
(where a
and b
are var
) is equivalent to foo = a
, a = b
, b = foo
, where foo
is a variable which is guaranteed not to exist yet, and not a = b
, b = a
(which would yield a
and b
both with the same value). I tried (a, b) = (b, a)
and it appears to work in parallel, but that's not the same as a documented description of the behavior.
Tuples are value types, and once constructed they are independent of the values that are part of them.
var a = 2
var b = 3
var t1 = (a, b)
print(t1) // (2, 3)
a = 4
print(t1) // (2, 3)
Thus the tuple (a, b)
carries the actual values of the two variables, which is why the (b, a) = (a, b)
assignment works without problems. Behind the scenes, the actual assigment is (b, a) = (2, 3)
(assuming a
and b
have the values from the above code snippet).