[swift-users] Why does the withUnsafeMutableBufferPointer closure take an inout parameter?

Martin R martinr448 at gmail.com
Wed Oct 11 16:23:32 CDT 2017


> On 11. Oct 2017, at 22:11, Guillaume Lessard <glessard at tffenterprises.com> wrote:
> 
> 
>> On Oct 10, 2017, at 01:50, Martin R via swift-users <swift-users at swift.org> wrote:
>> 
>> Thank you Howard for you response! However, I have a follow-up question:
>> 
>> Why are UnsafeMutableBufferPointer methods which modify not the buffer pointer itself, but only the pointed-to memory, mutating at all?
> 
> Don’t forget that UnsafeMutableBufferPointer conforms to MutableCollection, and that many MutableCollection methods (such as swapAt) must be mutating. The BufferPointer types are unusual among Collection conformers in not having value semantics.
> 
> Cheers,
> Guillaume Lessard
> 

But an implementation of a mutating protocol method does not have to be mutating:

    protocol Foo {
        mutating func test()
    }

    struct Bar: Foo {
        func test() { }
    }

    let bar = Bar()
    bar.test()

Isn't that a similar situation as with the subscript setter, which is mutating in general for MutableCollection, but non-mutating for UnsafeMutableBufferPointer?

Martin





More information about the swift-users mailing list