<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 9, 2017, at 8:27 AM, Jens Persson <<a href="mailto:jens@bitcycle.com" class="">jens@bitcycle.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">(Also, note that your implementation of == uses lhs === rhs thus will only return true when lhs and rhs are the same instance of SomeClass.)</div></div></blockquote><div>Of course — i threw that in just to make a simple example.</div><div><br class=""></div><div>Followup question: what I really wanted to write was an == operator for a tree:</div><div><br class=""></div><div>// silly tree, useful for nothing</div><div>class Tree : Equatable {</div><div> let rootData:Int</div><div> let children:[(String, Tree)]</div><div><br class=""></div><div> static public func ==(_ lhs:Tree, _ rhs:Tree) {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return lhs.rootData == rhs.rootData && </div><div> lhs.children == rhs.children<span class="Apple-tab-span" style="white-space:pre">                </span>// sadly, this doesn’t compile</div><div> }</div><div>}</div><div><br class=""></div><div>I.e. since now that tuples of (Int, Tree) can be compared, I thought, great, I get an incredibly elegant description: just compare the child arrays. But this doesn’t compile, and I assume that telling the compiler that (T,Tree) is a tuple that conforms to Equatable is just not happening in this lifetime?</div><div><br class=""></div><div>Not that it’s a big deal: I can of course write</div><div><div><br class=""></div><div> static public func ==(_ lhs:Tree, _ rhs:Tree) {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return lhs.rootData == rhs.rootData && lhs.children.count == rhs.children.count &&</div><div> all(lhs.children.indices.map { lhs.children[$0] == rhs.children[$0] }</div><div> }</div><div><br class=""></div><div>But still: it would be nice to not have to break it down manually. (all() is a free function doing what you would guess it does. i gave up resisting the urge and defined both any() and all(), after years of loving them in Python. I know I should just a more swift-like idiom, but dang it, it’s just too short. I would really love for any() and all() to become standard lib free functions. they’re so incredibly useful.)</div><div><br class=""></div></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="">/Jens</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sun, Jul 9, 2017 at 5:24 PM, Jens Persson <span dir="ltr" class=""><<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">Making SomeClass conform to Equatable should fix it:<div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(4,51,255)" class="">class</span> SomeClass : <span style="color:rgb(52,149,175)" class="">Equatable</span> {</div><span class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(4,51,255)" class="">static</span> <span style="color:rgb(4,51,255)" class="">public</span> <span style="color:rgb(4,51,255)" class="">func</span> ==(<span style="color:rgb(4,51,255)" class="">_</span> lhs:<span style="color:rgb(52,149,175)" class="">SomeClass</span>, <span style="color:rgb(4,51,255)" class="">_</span> rhs:<span style="color:rgb(52,149,175)" class="">SomeClass</span>) -> <span style="color:rgb(52,149,175)" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(4,51,255)" class="">return</span> lhs === rhs</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></span><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">/Jens</div></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote"><div class=""><div class="h5">On Sun, Jul 9, 2017 at 5:11 PM, David Baraff via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class=""><div class="h5"><div style="word-wrap:break-word" class="">Given 2-tuples of type (T1, T2), you should be able to invoke the == operator if you could on both types T1 and T2, right? i.e.<div class=""><br class=""></div><div class="">(“abc”, 3) == (“abc”, 4)<span class="m_5733283585223255130m_-8266073610500043056Apple-tab-span" style="white-space:pre-wrap">        </span>// legal</div><div class=""><br class=""></div><div class="">but:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">class SomeClass {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> static public func ==(_ lhs:</span><span style="font-variant-ligatures:no-common-ligatures;color:#9f35da" class="">SomeClass</span><span style="font-variant-ligatures:no-common-ligatures" class="">, _ rhs:</span><span style="font-variant-ligatures:no-common-ligatures;color:#9f35da" class="">SomeClass</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af" class="">Bool</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> return lhs === rhs</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">let c1 = </span><span style="font-variant-ligatures:no-common-ligatures;color:#9f35da" class="">SomeClass</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">let c2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:#9f35da" class="">SomeClass</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">let t1 = (</span><span style="font-variant-ligatures:no-common-ligatures;color:#b4261a" class="">"abc"</span><span style="font-variant-ligatures:no-common-ligatures" class="">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#0536ff" class="">c1</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">let t2 = (</span><span style="font-variant-ligatures:no-common-ligatures;color:#b4261a" class="">"abc"</span><span style="font-variant-ligatures:no-common-ligatures" class="">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#0536ff" class="">c2</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(5,54,255)" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">c1</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#9f35da" class="">==</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures" class="">c2<span class="m_5733283585223255130m_-8266073610500043056Apple-tab-span" style="white-space:pre-wrap">                </span>// legal</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#0536ff" class="">t1</span><span style="font-variant-ligatures:no-common-ligatures" class=""> == </span><span style="font-variant-ligatures:no-common-ligatures;color:#0536ff" class="">t2<span class="m_5733283585223255130m_-8266073610500043056Apple-tab-span" style="white-space:pre-wrap">                </span>// illegal</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#0536ff" class=""><br class=""></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><br class=""></div><div style="margin:0px;line-height:normal;min-height:13px" class="">Why is t1 == t2 not legal given that c1 == c2 IS legal?</div><div style="margin:0px;line-height:normal;min-height:13px" class=""><br class=""></div><div style="margin:0px;line-height:normal;min-height:13px" class=""><br class=""></div></div><br class=""></div></div>______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>