3 ms·
I'm assuming this is true var a = [1,2,3] var b = a b[0] = 10] > a [10,2,3] and so copy-on-assign only applies when assigning an immutabl
by tel 12y ago
I'm assuming this is true
var a = [1,2,3]
var b = a
b[0] = 10]
> a
[10,2,3]
and so copy-on-assign only applies when assigning an immutable reference to a mutable one.
- josephlord 12y agoNo a has the value [1,2,3] still in that scenario. Copy on assign semantics is for constants and mutable variables. Just tested in the playground: var a = [1,2,3] var b = a a == b // true a === b // true b[0] = 10 a // [1,2,3] b // [10,2,3] b[0] = 1 a == b // true a === b // false a // [1,2,3] b // [1,2,3]
- tel 12y agoAh! Cool! My original point was always supposing that held. Being that it doesn't then I agree with Swift being consistent here. Observing sharing using (===) is still dangerous, but presumably that can be saved by wrapping it in sufficient warning.
- josephlord 12y agoI wouldn't recommend using === in production (except for objects of classes) either but for this demonstration it let me show the copy on write without going to assembly. Glad we've reached a common understanding, I wasn't quite sure that I hadn't missed something.