<div dir="ltr">Pity nothing else works, looks like I am stuck with multiple wrappers.<div><br></div><div>I will echo Dave&#39;s, Tim&#39;s, and Jordan&#39;s thoughts, roll on <span style="font-size:12.8px">Conditional Conformance.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Thanks for your help.</span></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">  -- Howard.<br></div></div>
<br><div class="gmail_quote">On 17 November 2016 at 09:35, Jordan Rose <span dir="ltr">&lt;<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>&gt;</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"><div><div class="h5"><br><div><blockquote type="cite"><div>On Nov 16, 2016, at 7:35, David Sweeris via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_1145379205259842513Apple-interchange-newline"><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Nov 15, 2016, at 11:55 PM, Howard Lovatt &lt;<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>&gt; wrote:</div><br class="m_1145379205259842513Apple-interchange-newline"><div><div dir="ltr">@Dave,<div><br></div><div>How do I write that though.</div><div><br></div><div>I can&#39;t write:</div><div><br></div><div>    extension Array: Equatable {<br>        static func ==(lhs: Array, rhs: Array) -&gt; Bool {<br>            let size = lhs.count<br>            precondition(rhs.count == size, &quot;The arrays must be the same length&quot;)<br>            for i in 0 ..&lt; size {<br>                if (lhs[i] as! Equatable) != (rhs[i] as! Equatable) {<br>                    return false<br>                }<br>            }<br>            return true<br>        }<br>    }</div><div><br></div><div>Because I can&#39;t cast to an Equatable, because Equatable uses Self.</div><div><br></div><div>Am I missing something?</div><div><br></div><div> -- Howard.</div></div><div class="gmail_extra"><br clear="all"><div><div class="m_1145379205259842513gmail_signature" data-smartmail="gmail_signature">  -- Howard.<br></div></div>
<br><div class="gmail_quote">On 16 November 2016 at 16:35, David Sweeris <span dir="ltr">&lt;<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_1145379205259842513HOEnZb"><div class="m_1145379205259842513h5"><br>
&gt; On Nov 15, 2016, at 21:39, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi All,<br>
&gt;<br>
&gt; Does anyone have a good workaround for generics not currently supporting conditional conformance to a protocol. As stated in the Generics Manifesto something like this would be nice:<br>
&gt;<br>
&gt;   extension Array: Equatable where Element: Equatable {<br>
&gt;         static func ==(lhs: Array, rhs: Array) -&gt; Bool { ... }<br>
&gt;     }<br>
&gt;<br>
&gt; But I would currently write a wrapper, something like:<br>
&gt;<br>
&gt;     struct ArrayE&lt;T: Equatable&gt; {<br>
&gt;         var elements: [T]<br>
&gt;     }<br>
&gt;     extension ArrayE: Equatable {<br>
&gt;         static func ==(lhs: ArrayE, rhs: ArrayE) -&gt; Bool { ...  }<br>
&gt;     }<br>
&gt;<br>
&gt; This can get unwieldy when there are a lot of conditional protocol extensions required, i.e. wrappers round wrappers.<br>
&gt;<br>
&gt; Is there a better way?<br>
&gt;<br>
&gt; Thanks for any tips,<br>
&gt;<br>
&gt;   -- Howard.<br>
</div></div>&gt; ______________________________<wbr>_________________<br>
&gt; swift-users mailing list<br>
&gt; <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br>
Can you make Array conform to Equatable for any T and then in the == function, if T conforms to Equatable loop the Arrays to check if they&#39;re equal, and if it doesn&#39;t conform just return false?<br>
<br>
I mean, it&#39;s still &quot;wrong&quot;, but at least you won&#39;t get any false positives.<br>
<br>
- Dave Sweeris</blockquote></div><br></div>
</div></blockquote><br></div><div>You are correct. The work-around is to use two extensions and overload the == operator:</div><div><br></div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">extension</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures">Array</span><span style="font-variant-ligatures:no-common-ligatures">: </span><span style="font-variant-ligatures:no-common-ligatures">Equatable</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">static</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">func</span><span style="font-variant-ligatures:no-common-ligatures"> == (lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Array</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Array</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures">false</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">extension</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Array</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">where</span><span style="font-variant-ligatures:no-common-ligatures"> Element : Equatable {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">public</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">static</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">func</span><span style="font-variant-ligatures:no-common-ligatures"> == (lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Array</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Array</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">return</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">count</span><span style="font-variant-ligatures:no-common-ligatures"> == rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">count</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">for</span><span style="font-variant-ligatures:no-common-ligatures"> i </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">in</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">0</span><span style="font-variant-ligatures:no-common-ligatures">..&lt;lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">count</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">                </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">if</span><span style="font-variant-ligatures:no-common-ligatures"> lhs[i] != rhs[i] {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">                    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">false</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">                }</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            }</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">true</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }()</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;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>It works in playgrounds (Xcode 8.1 (8B62)), but I haven’t tested it outside a few trivial cases.</div></div></blockquote><br></div></div></div><div>This does not work. The == dispatch for Arrays is static in this case, not dynamic. You can test this by writing something generic on Equatable.</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>func same&lt;T: Equatable&gt;(_ x: T, _ y: T) -&gt; Bool { return x == y }</div><div>print(same([1], [2]))</div></blockquote><div><br></div>Rule of thumb: overloads are resolved statically, protocol requirements are invoked dynamically. You cannot get dynamic behavior out of just overloads, ever.<div><br></div><div>I don&#39;t think there&#39;s a way to get this behavior today, unfortunately.</div><span class="HOEnZb"><font color="#888888"><div><br><div>Jordan</div></div></font></span></div></blockquote></div><br></div>