<div style="white-space:pre-wrap">That&#39;s my plan (to replace my wrappers once conditional conformance is available). Good news that Conditional Conformance has just been approved.</div><br><div class="gmail_quote"><div dir="ltr">On Thu., 17 Nov. 2016 at 1:30 pm, David Sweeris &lt;<a href="mailto:davesweeris@mac.com">davesweeris@mac.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">On Nov 16, 2016, at 16:35, Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com" class="gmail_msg" target="_blank">jordan_rose@apple.com</a>&gt; wrote:<br class="gmail_msg"><br class="gmail_msg"></div><blockquote type="cite" class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Nov 16, 2016, at 7:35, David Sweeris via swift-users &lt;<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_-8202142620149713303Apple-interchange-newline gmail_msg"><div class="gmail_msg"><div style="word-wrap:break-word" class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Nov 15, 2016, at 11:55 PM, Howard Lovatt &lt;<a href="mailto:howard.lovatt@gmail.com" class="gmail_msg" target="_blank">howard.lovatt@gmail.com</a>&gt; wrote:</div><br class="m_-8202142620149713303Apple-interchange-newline gmail_msg"><div class="gmail_msg"><div dir="ltr" class="gmail_msg">@Dave,<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">How do I write that though.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I can&#39;t write:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">    extension Array: Equatable {<br class="gmail_msg">        static func ==(lhs: Array, rhs: Array) -&gt; Bool {<br class="gmail_msg">            let size = lhs.count<br class="gmail_msg">            precondition(rhs.count == size, &quot;The arrays must be the same length&quot;)<br class="gmail_msg">            for i in 0 ..&lt; size {<br class="gmail_msg">                if (lhs[i] as! Equatable) != (rhs[i] as! Equatable) {<br class="gmail_msg">                    return false<br class="gmail_msg">                }<br class="gmail_msg">            }<br class="gmail_msg">            return true<br class="gmail_msg">        }<br class="gmail_msg">    }</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Because I can&#39;t cast to an Equatable, because Equatable uses Self.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Am I missing something?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"> -- Howard.</div></div><div class="gmail_extra gmail_msg"><br clear="all" class="gmail_msg"><div class="gmail_msg"><div class="m_-8202142620149713303gmail_signature gmail_msg" data-smartmail="gmail_signature">  -- Howard.<br class="gmail_msg"></div></div>
<br class="gmail_msg"><div class="gmail_quote gmail_msg">On 16 November 2016 at 16:35, David Sweeris <span dir="ltr" class="gmail_msg">&lt;<a href="mailto:davesweeris@mac.com" class="gmail_msg" target="_blank">davesweeris@mac.com</a>&gt;</span> wrote:<br class="gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_-8202142620149713303HOEnZb gmail_msg"><div class="m_-8202142620149713303h5 gmail_msg"><br class="gmail_msg">
&gt; On Nov 15, 2016, at 21:39, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a>&gt; wrote:<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; Hi All,<br class="gmail_msg">
&gt;<br class="gmail_msg">
&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 class="gmail_msg">
&gt;<br class="gmail_msg">
&gt;   extension Array: Equatable where Element: Equatable {<br class="gmail_msg">
&gt;         static func ==(lhs: Array, rhs: Array) -&gt; Bool { ... }<br class="gmail_msg">
&gt;     }<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; But I would currently write a wrapper, something like:<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt;     struct ArrayE&lt;T: Equatable&gt; {<br class="gmail_msg">
&gt;         var elements: [T]<br class="gmail_msg">
&gt;     }<br class="gmail_msg">
&gt;     extension ArrayE: Equatable {<br class="gmail_msg">
&gt;         static func ==(lhs: ArrayE, rhs: ArrayE) -&gt; Bool { ...  }<br class="gmail_msg">
&gt;     }<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; This can get unwieldy when there are a lot of conditional protocol extensions required, i.e. wrappers round wrappers.<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; Is there a better way?<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt; Thanks for any tips,<br class="gmail_msg">
&gt;<br class="gmail_msg">
&gt;   -- Howard.<br class="gmail_msg">
</div></div>&gt; _______________________________________________<br class="gmail_msg">
&gt; swift-users mailing list<br class="gmail_msg">
&gt; <a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a><br class="gmail_msg">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="gmail_msg">
<br class="gmail_msg">
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 class="gmail_msg">
<br class="gmail_msg">
I mean, it&#39;s still &quot;wrong&quot;, but at least you won&#39;t get any false positives.<br class="gmail_msg">
<br class="gmail_msg">
- Dave Sweeris</blockquote></div><br class="gmail_msg"></div>
</div></blockquote><br class="gmail_msg"></div><div class="gmail_msg">You are correct. The work-around is to use two extensions and overload the == operator:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">extension</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">Array</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">: </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">Equatable</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">public</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">static</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> == (lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Array</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Array</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Bool</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">        </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">false</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">    }</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">extension</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Array</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">where</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> Element : Equatable {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">public</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">static</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> == (lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Array</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Array</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Bool</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">count</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> == rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">count</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81" class="gmail_msg">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">for</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> i </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">in</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">0</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">..&lt;lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">count</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">                </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">if</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> lhs[i] != rhs[i] {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">                    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">false</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">                }</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">            }</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">            </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">true</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">        }()</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">    }</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div><div class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></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 class="gmail_msg"></div><div class="gmail_msg">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 class="gmail_msg"><br class="gmail_msg"></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class="gmail_msg"><div class="gmail_msg">func same&lt;T: Equatable&gt;(_ x: T, _ y: T) -&gt; Bool { return x == y }</div><div class="gmail_msg">print(same([1], [2]))</div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div>Rule of thumb: overloads are resolved statically, protocol requirements are invoked dynamically. You cannot get dynamic behavior out of just overloads, ever.<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I don&#39;t think there&#39;s a way to get this behavior today, unfortunately.</div></blockquote><br class="gmail_msg"></div><div dir="auto" class="gmail_msg"><div class="gmail_msg">Aw, nuts! I forgot to try adding another level of, um... genericititty?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Yeah, I think you&#39;re right... On the plus side, when conditional conformance hits the wrapper structs can be backed out just by searching your project for their name and replacing it with &quot;Array&quot; (or &quot;Set&quot;, etc), right?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">- Dave Sweeris</div></div></blockquote></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature">-- Howard.</div>