<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">According to the latest Swift 3 docs</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:georgia,serif"><div class="gmail_default">“As an optimization, when the argument is a value stored at a physical address in memory, the same memory location is used both inside and outside the function body. The optimized behavior is known as call by reference; it satisfies all of the requirements of the copy-in copy-out model while removing the overhead of copying. Write your code using the model given by copy-in copy-out, without depending on the call-by-reference optimization, so that it behaves correctly with or without the optimization.”</div></div><div class="gmail_default" style="font-family:georgia,serif"><div class="gmail_default"><br></div></div><div class="gmail_default" style="font-family:georgia,serif"><div class="gmail_default">摘录来自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks. </div><div class="gmail_default"><br></div></div></blockquote><font face="georgia, serif"><div class="gmail_default" style="font-family:georgia,serif;display:inline">So the behavior is because of the optimization, which should not rely on.</div></font><div><font face="georgia, serif"><div class="gmail_default" style="font-family:georgia,serif;display:inline"><br></div></font></div><div><font face="georgia, serif"><div class="gmail_default" style="font-family:georgia,serif;display:inline">Zhaoxin </div><br></font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 14, 2016 at 5:12 AM, Jens Alfke 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"><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On Jun 13, 2016, at 1:43 PM, Joe Groff <<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>> wrote:</div><br><div><span style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">I see, missed that part. That's a bug—`acopy` should remain a distinct copy of `a`.</span><br style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"></div></blockquote></div><br></span><div>I’m guessing that when `a` is copied into `acopy`, the same array contents are shared between them, then when `a` is mutated it gets a new copy of the contents. Then the assignment to `b` writes into the original array contents now held by `acopy`.</div><div><br></div><div>Still seems like it’s caused by having aliased `inout` variables, but the effect is nastier. I can imagine horrible bugs where `acopy` gets passed around somewhere else and then changes, breaking the something-else that’s holding it. It could be hard to track that back to its original cause, especially if there are two different codebases involved.</div><div><br></div><div>—Jens</div></div><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>
<br></blockquote></div><br></div>