<div dir="ltr">This seems to work for me:<div><br></div><div>```</div><div><div>class Super: Equatable { </div><div> let x: Int</div><div> init(x: Int) {</div><div> self.x = x</div><div> }</div><div> func equals(_ rhs: Super) -> Bool { </div><div> return x == rhs.x </div><div> } </div><div> static func ==(lhs: Super, rhs: Super) -> Bool { </div><div> return lhs.equals(rhs) </div><div> } </div><div>} </div><div><br></div><div>class Sub: Super {</div><div> let y: Int</div><div> init(x: Int, y: Int) {</div><div> self.y = y</div><div> super.init(x: x)</div><div> }</div><div> override func equals(_ rhs: Super) -> Bool {</div><div> if let rhs = rhs as? Sub {</div><div> return y == rhs.y && super.equals(rhs)</div><div> }</div><div> return false</div><div> } </div><div>}</div><div><br></div><div>let a = Sub(x: 1, y: 1)</div><div>let b = Sub(x: 1, y: 2)</div><div>let c = Sub(x: 1, y: 1)</div></div><div><br></div><div>a == b // false, expected</div><div>a == c // true, expected</div><div>a != b // true, expected</div><div>a != c // false, expected</div><div>```</div><div><br></div><div>Additionally, when I made the change Joe suggested, your code also worked, so maybe there was an error when you updated it?</div><div><br></div><div>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">https://github.com/apple/swift/blob/master/stdlib/public/core/Equatable.swift#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><br></div><div><br></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Jan 18, 2017 at 8:52 AM Francisco Javier Fernández Toro via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</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="gmail_msg">Thank you for your answer Joe,<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">you are right the <font face="monospace, monospace" class="gmail_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="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">let a = Subclass(foo: 1, bar: 1)</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">let b = Subclass(foo: 1, bar: 2)</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg"><br class="gmail_msg"></font></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">(a == b) != (a != b) // Prints true</font></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg"><br class="gmail_msg"></font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">let x = SubclassWithDifferentOperator(foo: 1, bar: 1)</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">let y = SubclassWithDifferentOperator(foo: 1, bar: 2)</font></div><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg"><br class="gmail_msg"></font></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><font face="monospace, monospace" class="gmail_msg">(x == y) != (x != y) // Prints false</font></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">As you can see above if a subclass does not implement the global function <font face="monospace, monospace" class="gmail_msg">!=</font>, the equal operation seems to be broken.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">---</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Fran Fernandez</div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_extra gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg">On Wed, Jan 18, 2017 at 5:44 PM, Joe Groff <span dir="ltr" class="gmail_msg"><<a href="mailto:jgroff@apple.com" class="gmail_msg" target="_blank">jgroff@apple.com</a>></span> wrote:<br class="gmail_msg"><blockquote class="gmail_quote gmail_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_2457570553868603841gmail- gmail_msg"><br class="gmail_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="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg">
><br class="gmail_msg">
> Hi,<br class="gmail_msg">
><br class="gmail_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="gmail_msg">
><br class="gmail_msg">
> If you don't define you own subclass != operator, Swift compiler will use the super class to resolve that operation.<br class="gmail_msg">
><br class="gmail_msg">
> Is there any reason for that?<br class="gmail_msg">
<br class="gmail_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="gmail_msg">
<span class="m_2457570553868603841gmail- gmail_msg"><br class="gmail_msg">
class Subclass: Superclass {<br class="gmail_msg">
let bar: Int<br class="gmail_msg">
init(foo: Int, bar: Int) {<br class="gmail_msg">
self.bar = bar<br class="gmail_msg">
super.init(foo: foo)<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
</span> override func equal(to: Superclass) -> Bool {<br class="gmail_msg">
if let toSub = to as? Subclass {<br class="gmail_msg">
return bar == toSub.bar && super.equal(to: to)<br class="gmail_msg">
}<br class="gmail_msg">
return false<br class="gmail_msg">
}<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_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="gmail_msg" target="_blank">bugs.swift.org</a> about that?<br class="gmail_msg">
<span class="m_2457570553868603841gmail-HOEnZb gmail_msg"><font color="#888888" class="gmail_msg"><br class="gmail_msg">
-Joe</font></span></blockquote></div><br class="gmail_msg"></div></div></div>
_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div></div>