<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Ah... we will be missing hiding behind the warm embrace of message passing/dynamic dispatch :P<br><br>Sent from my iPhone</div><div><br>On 20 Jan 2017, at 19:24, Pierre Monod-Broca via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>The way I understand it, it's a bad idea to override == and != (or any infix operator) for Sub if Super has them and that's why the default implementation from Equatable only generates !=(Super, Super) and not !=(Sub, Sub) (and there is no ==(Sub, Sub) generated either).</div><div><br></div><div>And it's a bad idea because (without dynamic dispatch on both operands) it leads to unexpected behavior.</div><div><br></div><div>Considering :</div><div>```</div><div>func ==(lhs: Super, rhs: Super) -> Bool {</div><div> print("Super")</div><div> return true</div><div>}</div><div><br></div><div>func ==(lhs: Sub, rhs: Sub) -> Bool {</div><div> print("Sub")</div><div> return false</div><div>}</div><div><br></div><div>let a = Sub()</div><div>let b = Sub()</div><div>a == b // Sub</div><div>a as Super == b // Super</div><div>a == b as Super // Super</div><div>à as Super == b as Super // Super</div><div>```</div><div><br></div><div>One would compare the same objects and don't get the same result.</div><div><br></div><div><div><span style="background-color: rgba(255, 255, 255, 0);">Instead you have to check the dynamic type yourself.</span></div></div><div><br></div><div><br><div>Pierre</div></div><div><br>Le 20 janv. 2017 à 10:45, Francisco Javier Fernández Toro via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> a écrit :<br><br></div><blockquote type="cite"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 18, 2017 at 6:58 PM, Tony Allevato <span dir="ltr"><<a href="mailto:tony.allevato@gmail.com" target="_blank">tony.allevato@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Ok, this actually does feel a bit strange. The behavior you're seeing seems to be a consequence of [SE-0091](<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0091-improving-operators-in-protocols.md" target="_blank">https://github.com/<wbr>apple/swift-evolution/blob/<wbr>master/proposals/0091-<wbr>improving-operators-in-<wbr>protocols.md</a>), but it looks like you're seeing different behavior than what I described in the "Class types and inheritance" section of that proposal.<div><br></div><div>If Sub has `==(Sub, Sub)` implemented as a *static* function, I just tried it and it's *ignored* (`==(Super, Super)` gets called instead), even when the two actual arguments are known to be statically of type Sub. I think this is because of the way that proposal was implemented: when it sees that `Sub` extends `Super`, which conforms to `Equatable`, it appears that it's only looking for static overloads of `==` that are satisfied at the *point of conformance*, which would be `==(Super, Super)` (because `Super` conforms to `Equatable where Self == Super`). The wording of the proposal makes this case: "Then, we say that we do not consider an operator function if it implements a protocol requirement, because the requirement is a generalization of all of the operator functions that satisfy that requirement."</div><div><br></div><div>Contrarily, if you provide `==(Sub, Sub)` as a global function instead of a static one, it *does* get called. I think in this case, the type checker gets the whole set of candidate operators (which, unlike above, includes the global `==(Sub, Sub)`), and it gets used because it's a more specific match?</div><div><br></div></div></blockquote><div><br></div><div>FWIW, I've just changed both `==` functions to make them global, the the outcome is still the same, its using `==(Super,Super)` to resolve `!=(Sub,Sub)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div></div><div>Can someone from the core team chime in and say whether this is intentional behavior? It feels wrong that simply changing the location where the operator is defined would change the behavior like this.</div><div><br></div><div>FWIW, to avoid these sharp edges, there's no need to implement `==` for subtypes; since you have to use an overridable `equals` method anyway, just have the base type implement `==` to delegate to it, and then have subtypes override `equals` alone.</div><div><br></div></div><div class="HOEnZb"><div class="h5"><br><div class="gmail_quote"><div dir="ltr">On Wed, Jan 18, 2017 at 9:36 AM Francisco Javier Fernández Toro <<a href="mailto:fran@gokarumi.com" target="_blank">fran@gokarumi.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="ltr" class="m_-808359227615273680gmail_msg">Yeah guys, you are right, my code is busted, I was trying to point something different out:<div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">The next code is showing the possible issue. In theory to make a class Equatable, you just have to mark it with the Equatable protocol and implement `==` as a static function or as a global one.</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">If you don't override the equal method and you just invoke your super class equality method you'll get something like this: </div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">```</div><div class="m_-808359227615273680gmail_msg">class Superclass : Equatable {</div><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"> let foo: Int</div><div class="m_-808359227615273680gmail_msg"> </div><div class="m_-808359227615273680gmail_msg"> init(foo: Int) { self.foo = foo }</div><div class="m_-808359227615273680gmail_msg"> </div><div class="m_-808359227615273680gmail_msg"> func equal(to: Superclass) -> Bool {</div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"> return foo == to.foo</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg"> </div><div class="m_-808359227615273680gmail_msg"> static func == (lhs: Superclass, rhs: Superclass) -> Bool {</div><div class="m_-808359227615273680gmail_msg"> return lhs.equal(to: rhs)</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg">}</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg">class Subclass: Superclass {</div><div class="m_-808359227615273680gmail_msg"> let bar: Int</div><div class="m_-808359227615273680gmail_msg"> init(foo: Int, bar: Int) {</div><div class="m_-808359227615273680gmail_msg"> self.bar = bar</div><div class="m_-808359227615273680gmail_msg"> super.init(foo: foo)</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg"> </div><div class="m_-808359227615273680gmail_msg"> func equal(to: Subclass) -> Bool {</div><div class="m_-808359227615273680gmail_msg"> return bar == to.bar && super.equal(to: to)</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg"> </div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"> static func == (lhs: Subclass, rhs: Subclass) -> Bool {</div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"> return lhs.equal(to: rhs)</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg">}</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">class SubclassWithDifferentOperator: Subclass {</div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"> static func != (lhs: SubclassWithDifferentOperator, rhs: SubclassWithDifferentOperator) -> Bool {</div><div class="m_-808359227615273680gmail_msg"> return !(lhs.equal(to: rhs))</div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg">}</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">let a = Subclass(foo: 1, bar: 1)</div><div class="m_-808359227615273680gmail_msg">let b = Subclass(foo: 1, bar: 2)</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg">(a == b) != (a != b) // Prints: false, not expected</div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">let x = SubclassWithDifferentOperator(<wbr>foo: 1, bar: 1)</div><div class="m_-808359227615273680gmail_msg">let y = SubclassWithDifferentOperator(<wbr>foo: 1, bar: 2)</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div></div></div></div><div dir="ltr" class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg">(x == y) != (x != y) // Prints: true, expected</div></div></div><div class="m_-808359227615273680gmail_msg">```</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">So, after adding a couple of `print` statement in those equal method what I can see is that for Subclass, when you are need to call `!=` what Swift is doing is using `func ==(Superclass, Superclass)` and apply `!` as Tony has pointed out.</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">What I cannot understand is why is not using `func == (Subclass, Subclass)`</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">I hope it makes more sense now.</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">---</div><div class="m_-808359227615273680gmail_msg">Fran Fernandez</div></div><div class="gmail_extra m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"><div class="gmail_quote m_-808359227615273680gmail_msg">On Wed, Jan 18, 2017 at 6:13 PM, Tony Allevato <span dir="ltr" class="m_-808359227615273680gmail_msg"><<a href="mailto:tony.allevato@gmail.com" class="m_-808359227615273680gmail_msg" target="_blank">tony.allevato@gmail.com</a>></span> wrote:<br class="m_-808359227615273680gmail_msg"><blockquote class="gmail_quote m_-808359227615273680gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="m_-808359227615273680gmail_msg">This seems to work for me:<div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">```</div><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg">class Super: Equatable { </div><div class="m_-808359227615273680gmail_msg"> let x: Int</div><div class="m_-808359227615273680gmail_msg"> init(x: Int) {</div><div class="m_-808359227615273680gmail_msg"> self.x = x</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg"> func equals(_ rhs: Super) -> Bool { </div><div class="m_-808359227615273680gmail_msg"> return x == rhs.x </div><div class="m_-808359227615273680gmail_msg"> } </div><div class="m_-808359227615273680gmail_msg"> static func ==(lhs: Super, rhs: Super) -> Bool { </div><div class="m_-808359227615273680gmail_msg"> return lhs.equals(rhs) </div><div class="m_-808359227615273680gmail_msg"> } </div><div class="m_-808359227615273680gmail_msg">} </div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">class Sub: Super {</div><div class="m_-808359227615273680gmail_msg"> let y: Int</div><div class="m_-808359227615273680gmail_msg"> init(x: Int, y: Int) {</div><div class="m_-808359227615273680gmail_msg"> self.y = y</div><div class="m_-808359227615273680gmail_msg"> super.init(x: x)</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg"> override func equals(_ rhs: Super) -> Bool {</div><div class="m_-808359227615273680gmail_msg"> if let rhs = rhs as? Sub {</div><div class="m_-808359227615273680gmail_msg"> return y == rhs.y && super.equals(rhs)</div><div class="m_-808359227615273680gmail_msg"> }</div><div class="m_-808359227615273680gmail_msg"> return false</div><div class="m_-808359227615273680gmail_msg"> } </div><div class="m_-808359227615273680gmail_msg">}</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">let a = Sub(x: 1, y: 1)</div><div class="m_-808359227615273680gmail_msg">let b = Sub(x: 1, y: 2)</div><div class="m_-808359227615273680gmail_msg">let c = Sub(x: 1, y: 1)</div></div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">a == b // false, expected</div><div class="m_-808359227615273680gmail_msg">a == c // true, expected</div><div class="m_-808359227615273680gmail_msg">a != b // true, expected</div><div class="m_-808359227615273680gmail_msg">a != c // false, expected</div><div class="m_-808359227615273680gmail_msg">```</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">Additionally, when I made the change Joe suggested, your code also worked, so maybe there was an error when you updated it?</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg">FWIW, the default implementation of != just invokes !(a == b) <<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Equatable.swift#L179-L181" class="m_-808359227615273680gmail_msg" target="_blank">https://github.com/apple/<wbr>swift/blob/master/stdlib/<wbr>public/core/Equatable.swift#<wbr>L179-L181</a>>, so I believe it's *impossible* (well, uh, barring busted RAM or processor I guess) for it to return the wrong value for the same arguments if you only implement ==.</div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680gmail_msg"><br class="m_-808359227615273680gmail_msg"></div><br class="m_-808359227615273680gmail_msg"><div class="gmail_quote m_-808359227615273680gmail_msg"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422h5 m_-808359227615273680gmail_msg"><div dir="ltr" class="m_-808359227615273680gmail_msg">On Wed, Jan 18, 2017 at 8:52 AM Francisco Javier Fernández Toro via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="m_-808359227615273680gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="m_-808359227615273680gmail_msg"></div></div></div><blockquote class="gmail_quote m_-808359227615273680gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422h5 m_-808359227615273680gmail_msg"><div dir="ltr" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">Thank you for your answer Joe,<div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">you are right the <font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">equal(to:)</font> wasn't a valid override, but even after using the one you've proposed, the behavior is not the expected one</div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div></div><div dir="ltr" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">let a = Subclass(foo: 1, bar: 1)</font></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">let b = Subclass(foo: 1, bar: 2)</font></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></font></div></div></div><div dir="ltr" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">(a == b) != (a != b) // Prints true</font></div></div></div><div dir="ltr" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></font></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">let x = SubclassWithDifferentOperator(<wbr>foo: 1, bar: 1)</font></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">let y = SubclassWithDifferentOperator(<wbr>foo: 1, bar: 2)</font></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></font></div></div></div><div dir="ltr" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">(x == y) != (x != y) // Prints false</font></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">As you can see above if a subclass does not implement the global function <font face="monospace, monospace" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">!=</font>, the equal operation seems to be broken.</div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">---</div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">Fran Fernandez</div></div></div><div dir="ltr" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="gmail_extra m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><div class="gmail_quote m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">On Wed, Jan 18, 2017 at 5:44 PM, Joe Groff <span dir="ltr" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><<a href="mailto:jgroff@apple.com" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg" target="_blank">jgroff@apple.com</a>></span> wrote:<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><blockquote class="gmail_quote m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047m_2457570553868603841gmail- m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
> On Jan 18, 2017, at 2:59 AM, Francisco Javier Fernández Toro via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
> Hi,<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
> I've found that when you have a class hierarchy which implements Equatable, if you want to have the != operator working as expected, you need to override it, it's not enough with ==.<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
> If you don't define you own subclass != operator, Swift compiler will use the super class to resolve that operation.<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
> Is there any reason for that?<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
</span>The `equal(to:)` method inside `Subclass` is not a valid override of `Superclass` because its argument only accepts `Subclass` instances, but the parent method needs to work with all `Superclass` instances. If you write it as an override, it should work:<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
<span class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047m_2457570553868603841gmail- m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
class Subclass: Superclass {<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
let bar: Int<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
init(foo: Int, bar: Int) {<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
self.bar = bar<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
super.init(foo: foo)<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
}<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
</span> override func equal(to: Superclass) -> Bool {<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
if let toSub = to as? Subclass {<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
return bar == toSub.bar && super.equal(to: to)<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
}<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
return false<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
}<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
}<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
We should probably raise an error, or at least a warning, instead of silently accepting your code as an overload. Would you be able to file a bug on <a href="http://bugs.swift.org" rel="noreferrer" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg" target="_blank">bugs.swift.org</a> about that?<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
<span class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047m_2457570553868603841gmail-HOEnZb m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><font color="#888888" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
-Joe</font></span></blockquote></div><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg"></div></div></div></div></div><span class="m_-808359227615273680gmail_msg">
______________________________<wbr>_________________<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
swift-evolution mailing list<br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br class="m_-808359227615273680m_-4542951819186758422m_4931755600843731047gmail_msg m_-808359227615273680gmail_msg">
</span></blockquote></div></div>
</blockquote></div><br class="m_-808359227615273680gmail_msg"></div>
</blockquote></div>
</div></div></blockquote></div><br></div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>