[swift-users] Understanding pass-by-value

Ryan Lovelett ryan at lovelett.me
Fri Nov 4 07:59:10 CDT 2016


struct Foo {
  init(from buffer: Data) {
     bar = integer(withBytes: Array(buffer[4..<6]))
     baz = integer(withBytes: Array(buffer[6..<8]))
     ...
  }

let d = Data(count: Int(3e+8))
let f = Foo(from: d)

Did I just make two copies of the `Data`? How would I investigate this
to understand it?

I _think_ that if I made it `inout` then it would not make a copy but
now the data buffer is mutable. I want it to be clear I'm not mutating
the buffer inside the initializer but I also want to be sure I'm not
making a copy of the buffer either.

Help?

-- 
  Ryan Lovelett
  ryan at lovelett.me


More information about the swift-users mailing list