I've been thinking about this over and over.
Sometimes, I think I get it, but then I lose it and I struggle to see how this assignment works.
var a = 1
var b = 2
(a, b) = (b, a)
print(a, b)
// Outputs 2 1
I first saw this in a Python discussion about Tuples, and it was until then just a simple value exchange problem. You have a and b, but you want a to become b and b to become a.
The Tuple allows this in a single expression, whereas usually you would use a temporary value:
var a = 1
var b = 2
let temp = a
a = b
b = temp
But the great thing with the tuple approach is we can eliminate the temporary variable and the need for 3 lines of code and just do:
(a, b) = (b, a)
So when I encountered Swift and found the same behaviour to be true I was initially happy. But when asked to explain what was happening, I began to realise I truly didn't know.
Well, perhaps I should clarify. The fact that it worked meant I understood what was happening, but I couldn't really explain why it was like that.
"A tuple is a lightweight collection type".
So what does:
(a, b) = (b, a)
actually mean when a Tuple is a lightweight collection?
The Value of b is placed into the value container a, and correspondingly the value of a is placed into the value container b. This is what does actually happen.
But it seems to break a lot of what is known about Swift.
If a tuple is a value type, then the a and b within the tuples would be copies, so the subsequent print of the original variables would print their original values, not the swapped values. So Tuples clearly are not value types.
This is in line with the definition that Tuples are "lightweight collection types". So far, so good,
So what does:
(a, b) = (b, a)
actually mean to a lightweight collection?
Surely it must mean that the collection (a, b) is replaced by the collection (b, a).
But if that was actually the case, a would still print as 1 and b would still print as 2. But somehow, a has been assigned the value of b.
What is the somehow that did that?