<div style="white-space:pre-wrap">That'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 <<a href="mailto:davesweeris@mac.com">davesweeris@mac.com</a>> 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 <<a href="mailto:jordan_rose@apple.com" class="gmail_msg" target="_blank">jordan_rose@apple.com</a>> 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 <<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a>> 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 <<a href="mailto:howard.lovatt@gmail.com" class="gmail_msg" target="_blank">howard.lovatt@gmail.com</a>> 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'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) -> Bool {<br class="gmail_msg"> let size = lhs.count<br class="gmail_msg"> precondition(rhs.count == size, "The arrays must be the same length")<br class="gmail_msg"> for i in 0 ..< 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'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"><<a href="mailto:davesweeris@mac.com" class="gmail_msg" target="_blank">davesweeris@mac.com</a>></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">
> On Nov 15, 2016, at 21:39, Howard Lovatt via swift-users <<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a>> wrote:<br class="gmail_msg">
><br class="gmail_msg">
> Hi All,<br class="gmail_msg">
><br class="gmail_msg">
> 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">
><br class="gmail_msg">
> extension Array: Equatable where Element: Equatable {<br class="gmail_msg">
> static func ==(lhs: Array, rhs: Array) -> Bool { ... }<br class="gmail_msg">
> }<br class="gmail_msg">
><br class="gmail_msg">
> But I would currently write a wrapper, something like:<br class="gmail_msg">
><br class="gmail_msg">
> struct ArrayE<T: Equatable> {<br class="gmail_msg">
> var elements: [T]<br class="gmail_msg">
> }<br class="gmail_msg">
> extension ArrayE: Equatable {<br class="gmail_msg">
> static func ==(lhs: ArrayE, rhs: ArrayE) -> Bool { ... }<br class="gmail_msg">
> }<br class="gmail_msg">
><br class="gmail_msg">
> This can get unwieldy when there are a lot of conditional protocol extensions required, i.e. wrappers round wrappers.<br class="gmail_msg">
><br class="gmail_msg">
> Is there a better way?<br class="gmail_msg">
><br class="gmail_msg">
> Thanks for any tips,<br class="gmail_msg">
><br class="gmail_msg">
> -- Howard.<br class="gmail_msg">
</div></div>> _______________________________________________<br class="gmail_msg">
> swift-users mailing list<br class="gmail_msg">
> <a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a><br class="gmail_msg">
> <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're equal, and if it doesn't conform just return false?<br class="gmail_msg">
<br class="gmail_msg">
I mean, it's still "wrong", but at least you won'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">) -> </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">) -> </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">&&</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">..<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<T: Equatable>(_ x: T, _ y: T) -> 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't think there'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'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 "Array" (or "Set", 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>