News for hackers. Stuff that matters.
Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by Someone on Tuesday April 09, 2024 @04:01AM (#39977116)
> 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],[],[],[]]
Re:Why Do Python Lists Multiply Oddly? Exploring the CPython... by mypalmike on Tuesday April 09, 2024 @05:31AM (#39977702)
Just teaching myself Swift this week. Thanks, this is helpful!