[swift-users] inout params seem to have undefined behavior

Brent Royal-Gordon brent at architechies.com
Sat Jun 11 18:05:19 CDT 2016


> My recollection is that in Swift the subscript operator (`arr[2]` in this case) can refer to the setter xor the getter, but not both within the same statement.

Quite to the contrary. Rather than using the setter directly, Swift often uses `materializeForSet`, a combined get-and-set operation which is much more efficient, particularly when assigning directly into arrays. To keep from having to use very slow access all the time, it imposes a rule (which is not and cannot be enforced by the compiler) that you can't hold two mutable references to overlapping storage simultaneously, or they may do strange things like lose some of the writes you make.

Here's an old design document discussing some things in this area: <https://github.com/apple/swift/blob/73841a643c087e854a2f62c7e073317bd43af310/docs/proposals/Accessors.rst> I'm not sure how authoritative it is, but it might give you an idea of what's going on.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-users mailing list