<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="">On 28 Apr 2016, Dave Abrahams wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" style="font-family: Menlo-Regular; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I think I can guess what you wanted the above to mean but, mind you, the<br class="">in-place sort returns `()` so you wouldn't chain its result like that. On the<br class="">other hand, the above code already works using the non-mutating `.sort()` (to be<br class="">known as `.sorted()` in Swift 3), and—correct me if I'm wrong—the compiler<br class="">probably optimises the copy away using copy-on-write anyway.<br class=""></blockquote><br style="font-family: Menlo-Regular; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Menlo-Regular; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">No, the compiler can't automatically turn non-mutating operations into</span><br style="font-family: Menlo-Regular; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Menlo-Regular; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">in-place operations.</span><br style="font-family: Menlo-Regular; font-size: 11px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote></div><br class=""><div class="">I'm talking about `<font face="Menlo" class=""><span style="font-size: 11px;" class="">.sorted()</span></font>` in particular. Its&nbsp;<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/CollectionAlgorithms.swift.gyb#L329-L335" class="">implementation</a>&nbsp;is essentially this:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span><b style="font-size: 11px;" class="">var</b><span style="font-size: 11px;" class="">&nbsp;result&nbsp;=&nbsp;ContiguousArray(</span><b style="font-size: 11px;" class="">self</b><span style="font-size: 11px;" class="">) <font color="#919191" class="">// What if `self` is not used after this line?</font></span><br class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; result.sort()</span><br class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span><b style="font-size: 11px;" class="">return</b><span style="font-size: 11px;" class="">&nbsp;Array(result)</span></font></div><div class=""><br class=""></div><div class="">Depending on what `<b style="font-family: Menlo; font-size: 11px;" class="">self</b>&nbsp;` is and how copy-on-write works, if `<font face="Menlo" class=""><span style="font-size: 11px;" class="">foo().sorted()</span></font>` was called on a temporary (unique) <font face="Menlo" class=""><span style="font-size: 11px;" class="">Array</span></font> returned by `<font face="Menlo" class=""><span style="font-size: 11px;" class="">foo()</span></font>`, I believe the initialisation `<span style="font-family: Menlo; font-size: 11px;" class="">ContiguousArray</span><span style="font-family: Menlo; font-size: 11px;" class="">(</span><b style="font-family: Menlo; font-size: 11px;" class="">self</b><span style="font-family: Menlo; font-size: 11px;" class="">)</span>` could be able to reuse `<font face="Menlo" class=""><span style="font-size: 11px;" class=""><b class="">self</b>._buffer</span></font>` instead of making a copy. Whether it indeed does that, remains unclear to me,&nbsp;<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Arrays.swift.gyb#L925-L930" class="">probably not</a>.</div><div class=""><br class=""></div><div class="">— Pyry</div><div class=""><br class=""></div></body></html>