Actually, `swapAt` does have a precondition that the elements differ.<br><div class="gmail_quote"><div dir="ltr">On Sun, May 7, 2017 at 09:41 Paul Cantrell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On May 7, 2017, at 6:01 AM, Jean-Daniel &lt;<a href="mailto:mailing@xenonium.com" target="_blank">mailing@xenonium.com</a>&gt; wrote:</div><br class="m_-4916738634454727206Apple-interchange-newline"><div><div style="font-family:HelveticaNeue;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div><br class="m_-4916738634454727206Apple-interchange-newline">Le 7 mai 2017 à 03:54, Paul Cantrell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="m_-4916738634454727206Apple-interchange-newline"><div><div style="word-wrap:break-word"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On May 6, 2017, at 12:36 PM, John McCall via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-4916738634454727206Apple-interchange-newline"><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On May 6, 2017, at 1:11 PM, Félix Cloutier via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-4916738634454727206Apple-interchange-newline"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Concern: `swap` is quoted a lot for a method that would break under this rule, but as it happens, `swap` with the same value on both sides is (should be) a no-op. Is there a way to not trip the static or dynamic checkers in well-defined cases like that one? Is there any way to check that two inout parameters refer to the same value?</div></div></blockquote></div><div><div><br></div>The only reasonable way to do this is statically, and why would you call &#39;swap&#39; statically with the same l-value for both arguments?</div></div></div></blockquote><div><br></div><div>When static analysis can determine that a swap is<span class="m_-4916738634454727206Apple-converted-space"> </span><i>always</i> a noop, I can’t see any reason not to flag it as an error.</div><div><br></div><div>But Félix’s question was also about the runtime case. And he has a good point.</div><div><br></div><div>Here’s a compelling example where allowing the noop swap would make sense:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(50,62,125)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures">extension</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(88,126,168)">Array</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span>{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">     <span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(50,62,125)">mutating</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(50,62,125)">func</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span>shuffle() {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">       <span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(50,62,125)">for</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span>i<span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(50,62,125)">in</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(88,126,168)">indices</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span>{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(88,126,168)"><span style="font-variant-ligatures:no-common-ligatures">         <span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(50,62,125)">let</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span>j = i +<span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures">Int</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures">arc4random_uniform</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures">UInt32</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures">count</span><span style="font-variant-ligatures:no-common-ligatures"><span class="m_-4916738634454727206Apple-converted-space"> </span>- i)))</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">         <span class="m_-4916738634454727206Apple-converted-space"> </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(88,126,168)">swap</span><span style="font-variant-ligatures:no-common-ligatures">(&amp;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(50,62,125)">self</span><span style="font-variant-ligatures:no-common-ligatures">[i], &amp;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(50,62,125)">self</span><span style="font-variant-ligatures:no-common-ligatures">[j])</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">      }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div></div></div></div></div></blockquote><div><br></div>Isn’t this issue already solved by the introduction of swapAt ? </div></div></blockquote></div><br></div><div style="word-wrap:break-word"><div>Ah, indeed, you’re quite right: I see that SE-0173 removes the “different elements” precondition for swapAt().</div><div><br></div><div>It’s not hard to imagine analogous situations involving data structures other than arrays, so presumably Félix’s point still stands in principle for the original swap(). But I notice that SE-0173 says swap() will be deprecated &amp; removed in the future?! Surprising, but well of topic: SE-0176 would allow the implementation of swapAt-like methods for other data structures, so no new concerns about this proposal as it stands.</div><div><br></div><div>Cheers, P</div><div><br></div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>