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

Karl Pickett karl.pickett at gmail.com
Sat Jun 11 20:27:34 CDT 2016


Wow there are some real doozy inout code examples in there, showing
aliasing much more fun than my snippet.  Unfortunately I can't
understand anything else the doc is talking about.  I guess I'll just
say a prayer and throw salt over my shoulder if using inout.

On Sat, Jun 11, 2016 at 6:05 PM, Brent Royal-Gordon
<brent at architechies.com> wrote:
>> 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