<div dir="ltr">Pity nothing else works, looks like I am stuck with multiple wrappers.<div><br></div><div>I will echo Dave's, Tim's, and Jordan'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"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</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"><div><div class="h5"><br><div><blockquote type="cite"><div>On Nov 16, 2016, at 7:35, David Sweeris via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> 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 <<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>> 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't write:</div><div><br></div><div> extension Array: Equatable {<br> static func ==(lhs: Array, rhs: Array) -> Bool {<br> let size = lhs.count<br> precondition(rhs.count == size, "The arrays must be the same length")<br> for i in 0 ..< 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'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"><<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>></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>
> On Nov 15, 2016, at 21:39, Howard Lovatt via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br>
><br>
> Hi All,<br>
><br>
> 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>
><br>
> extension Array: Equatable where Element: Equatable {<br>
> static func ==(lhs: Array, rhs: Array) -> Bool { ... }<br>
> }<br>
><br>
> But I would currently write a wrapper, something like:<br>
><br>
> struct ArrayE<T: Equatable> {<br>
> var elements: [T]<br>
> }<br>
> extension ArrayE: Equatable {<br>
> static func ==(lhs: ArrayE, rhs: ArrayE) -> Bool { ... }<br>
> }<br>
><br>
> This can get unwieldy when there are a lot of conditional protocol extensions required, i.e. wrappers round wrappers.<br>
><br>
> Is there a better way?<br>
><br>
> Thanks for any tips,<br>
><br>
> -- Howard.<br>
</div></div>> ______________________________<wbr>_________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
> <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're equal, and if it doesn't conform just return false?<br>
<br>
I mean, it's still "wrong", but at least you won'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">) -> </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">) -> </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">&&</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">..<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<T: Equatable>(_ x: T, _ y: T) -> 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't think there'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>