3 ms·
> If you say it’s the second (I can’t think of any language that does), then `a = […]; b = a` is an expensive copy by value assignment In Swift collections are
by Someone 3y ago
> If you say it’s the second (I can’t think of any language that does), then `a = […]; b = a` is an expensive copy by value assignment
In Swift collections are value types, so b = a conceptually is a copy. It’s not expensive, though, because Swift does copy-on-write.
You pay for that by a combination of having more expensive collection updates (they, at least conceptually, have to check whether to make that copy) and a more complex compiler (it tries to avoid having to actually do those checks at runtime)
var a: [Int]=[]
var b = [a,a,a,a]
b[0].append(1)
print(b)
will produce
[[1],[],[],[]]
- mypalmike 3y agoJust teaching myself Swift this week. Thanks, this is helpful!