<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">inout says it will copy into the function and copy back after the function is finished. But the order is unknown. So I think the example, </div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:georgia,serif"><span style="font-size:13px;font-family:arial,sans-serif">print(acopy) // prints "[1, 2, 99]" (e.g. a let variable changed!)</span></div></blockquote>i<div class="gmail_default" style="font-family:georgia,serif;display:inline">s a bug as the value changed inside of the function, before the function returns.</div><div><font face="georgia, serif"><br></font></div><div><font face="georgia, serif"><div class="gmail_default" style="font-family:georgia,serif;display:inline">Swift doc:</div></font></div><div><font face="georgia, serif"><div class="gmail_default" style="font-family:georgia,serif;display:inline"><br></div></font></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="georgia, serif"><div class="gmail_default" style="font-family:georgia,serif;display:inline"></div></font>“In-out parameters are passed as follows:</div><div><div><br></div></div><div><div>1<div class="gmail_default" style="font-family:georgia,serif;display:inline">. </div>When the function is called, the value of the argument is copied.</div></div><div><div>2<div class="gmail_default" style="font-family:georgia,serif;display:inline">. </div>In the body of the function, the copy is modified.</div></div><div><div>3<div class="gmail_default" style="font-family:georgia,serif;display:inline"> .</div>When the function returns, the copy’s value is assigned to the original argument.”</div></div><div><div><br></div></div><div><div>摘录来自: Apple Inc. “The Swift Programming Language (Swift 2.2)”。 iBooks. </div></div><div><br></div></blockquote><div><div><div class="gmail_default" style="font-family:georgia,serif;display:inline">Zhaoxin</div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 12, 2016 at 9:27 AM, Karl Pickett via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Wow there are some real doozy inout code examples in there, showing<br>
aliasing much more fun than my snippet. Unfortunately I can't<br>
understand anything else the doc is talking about. I guess I'll just<br>
say a prayer and throw salt over my shoulder if using inout.<br>
<div class="HOEnZb"><div class="h5"><br>
On Sat, Jun 11, 2016 at 6:05 PM, Brent Royal-Gordon<br>
<<a href="mailto:brent@architechies.com">brent@architechies.com</a>> wrote:<br>
>> 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.<br>
><br>
> 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.<br>
><br>
> Here's an old design document discussing some things in this area: <<a href="https://github.com/apple/swift/blob/73841a643c087e854a2f62c7e073317bd43af310/docs/proposals/Accessors.rst" rel="noreferrer" target="_blank">https://github.com/apple/swift/blob/73841a643c087e854a2f62c7e073317bd43af310/docs/proposals/Accessors.rst</a>> I'm not sure how authoritative it is, but it might give you an idea of what's going on.<br>
><br>
> --<br>
> Brent Royal-Gordon<br>
> Architechies<br>
><br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>