<div dir="ltr">Ok, this actually does feel a bit strange. The behavior you&#39;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">https://github.com/apple/swift-evolution/blob/master/proposals/0091-improving-operators-in-protocols.md</a>), but it looks like you&#39;re seeing different behavior than what I described in the &quot;Class types and inheritance&quot; section of that proposal.<div><br></div><div>If Sub has `==(Sub, Sub)` implemented as a *static* function, I just tried it and it&#39;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&#39;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: &quot;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.&quot;</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&#39;s a more specific match?</div><div><br></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&#39;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><br><div class="gmail_quote"><div dir="ltr">On Wed, Jan 18, 2017 at 9:36 AM Francisco Javier Fernández Toro &lt;<a href="mailto:fran@gokarumi.com">fran@gokarumi.com</a>&gt; 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">Yeah guys, you are right, my code is busted, I was trying to point something different out:<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_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="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">If you don&#39;t override the equal method and you just invoke your super class equality method you&#39;ll get something like this: </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">```</div><div class="gmail_msg">class Superclass : Equatable {</div><div class="gmail_msg"><div class="gmail_msg"></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">    let foo: Int</div><div class="gmail_msg">    </div><div class="gmail_msg">    init(foo: Int) { self.foo = foo }</div><div class="gmail_msg">    </div><div class="gmail_msg">    func equal(to: Superclass) -&gt; Bool {</div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">        return foo == to.foo</div><div class="gmail_msg">    }</div><div class="gmail_msg">    </div><div class="gmail_msg">    static func == (lhs: Superclass, rhs: Superclass) -&gt; Bool {</div><div class="gmail_msg">        return lhs.equal(to: rhs)</div><div class="gmail_msg">    }</div><div class="gmail_msg">}</div><div class="gmail_msg"><br class="gmail_msg"></div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">class Subclass: Superclass {</div><div class="gmail_msg">    let bar: Int</div><div class="gmail_msg">    init(foo: Int, bar: Int) {</div><div class="gmail_msg">        self.bar = bar</div><div class="gmail_msg">        super.init(foo: foo)</div><div class="gmail_msg">    }</div><div class="gmail_msg">    </div><div class="gmail_msg">    func equal(to: Subclass) -&gt; Bool {</div><div class="gmail_msg">        return bar == to.bar &amp;&amp; super.equal(to: to)</div><div class="gmail_msg">    }</div><div class="gmail_msg">    </div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">    static func == (lhs: Subclass, rhs: Subclass) -&gt; Bool {</div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">        return lhs.equal(to: rhs)</div><div class="gmail_msg">    }</div><div class="gmail_msg">}</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">class SubclassWithDifferentOperator: Subclass {</div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">    static func != (lhs: SubclassWithDifferentOperator, rhs: SubclassWithDifferentOperator) -&gt; Bool {</div><div class="gmail_msg">        return !(lhs.equal(to: rhs))</div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">    }</div><div class="gmail_msg">}</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">let a = Subclass(foo: 1, bar: 1)</div><div class="gmail_msg">let b = Subclass(foo: 1, bar: 2)</div><div class="gmail_msg"><br class="gmail_msg"></div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">(a == b) != (a != b) // Prints: false, not expected</div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">let x = SubclassWithDifferentOperator(foo: 1, bar: 1)</div><div class="gmail_msg">let y = SubclassWithDifferentOperator(foo: 1, bar: 2)</div><div class="gmail_msg"><br class="gmail_msg"></div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">(x == y) != (x != y) // Prints: true, expected</div></div></div><div class="gmail_msg">```</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_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="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">What I cannot understand is why is not using `func == (Subclass, Subclass)`</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I hope it makes more sense now.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">---</div><div class="gmail_msg">Fran Fernandez</div></div><div class="gmail_extra gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg">On Wed, Jan 18, 2017 at 6:13 PM, Tony Allevato <span dir="ltr" class="gmail_msg">&lt;<a href="mailto:tony.allevato@gmail.com" class="gmail_msg" target="_blank">tony.allevato@gmail.com</a>&gt;</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 dir="ltr" class="gmail_msg">This seems to work for me:<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">```</div><div class="gmail_msg"><div class="gmail_msg">class Super: Equatable { </div><div class="gmail_msg">    let x: Int</div><div class="gmail_msg">    init(x: Int) {</div><div class="gmail_msg">        self.x = x</div><div class="gmail_msg">    }</div><div class="gmail_msg">    func equals(_ rhs: Super) -&gt; Bool { </div><div class="gmail_msg">        return x == rhs.x </div><div class="gmail_msg">    } </div><div class="gmail_msg">    static func ==(lhs: Super, rhs: Super) -&gt; Bool { </div><div class="gmail_msg">        return lhs.equals(rhs) </div><div class="gmail_msg">    } </div><div class="gmail_msg">} </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">class Sub: Super {</div><div class="gmail_msg">    let y: Int</div><div class="gmail_msg">    init(x: Int, y: Int) {</div><div class="gmail_msg">        self.y = y</div><div class="gmail_msg">        super.init(x: x)</div><div class="gmail_msg">    }</div><div class="gmail_msg">    override func equals(_ rhs: Super) -&gt; Bool {</div><div class="gmail_msg">        if let rhs = rhs as? Sub {</div><div class="gmail_msg">            return y == rhs.y &amp;&amp; super.equals(rhs)</div><div class="gmail_msg">        }</div><div class="gmail_msg">        return false</div><div class="gmail_msg">    }   </div><div class="gmail_msg">}</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">let a = Sub(x: 1, y: 1)</div><div class="gmail_msg">let b = Sub(x: 1, y: 2)</div><div class="gmail_msg">let c = Sub(x: 1, y: 1)</div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">a == b  // false, expected</div><div class="gmail_msg">a == c  // true, expected</div><div class="gmail_msg">a != b  // true, expected</div><div class="gmail_msg">a != c  // false, expected</div><div class="gmail_msg">```</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_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="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">FWIW, the default implementation of != just invokes !(a == b) &lt;<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Equatable.swift#L179-L181" class="gmail_msg" target="_blank">https://github.com/apple/swift/blob/master/stdlib/public/core/Equatable.swift#L179-L181</a>&gt;, so I believe it&#39;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="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg"><div class="m_-4542951819186758422h5 gmail_msg"><div dir="ltr" class="gmail_msg">On Wed, Jan 18, 2017 at 8:52 AM Francisco Javier Fernández Toro via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="gmail_msg"></div></div></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_msg"><div class="m_-4542951819186758422h5 gmail_msg"><div dir="ltr" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">Thank you for your answer Joe,<div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">you are right the <font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">equal(to:)</font> wasn&#39;t a valid override, but even after using the one you&#39;ve proposed, the behavior is not the expected one</div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div></div><div dir="ltr" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">let a = Subclass(foo: 1, bar: 1)</font></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">let b = Subclass(foo: 1, bar: 2)</font></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></font></div></div></div><div dir="ltr" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">(a == b) != (a != b) // Prints true</font></div></div></div><div dir="ltr" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></font></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">let x = SubclassWithDifferentOperator(foo: 1, bar: 1)</font></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">let y = SubclassWithDifferentOperator(foo: 1, bar: 2)</font></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></font></div></div></div><div dir="ltr" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">(x == y) != (x != y) // Prints false</font></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">As you can see above if a subclass does not implement the global function <font face="monospace, monospace" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">!=</font>, the equal operation seems to be broken.</div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">---</div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">Fran Fernandez</div></div></div><div dir="ltr" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="gmail_extra m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><div class="gmail_quote m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">On Wed, Jan 18, 2017 at 5:44 PM, Joe Groff <span dir="ltr" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">&lt;<a href="mailto:jgroff@apple.com" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg" target="_blank">jgroff@apple.com</a>&gt;</span> wrote:<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><blockquote class="gmail_quote m_-4542951819186758422m_4931755600843731047gmail_msg 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_-4542951819186758422m_4931755600843731047m_2457570553868603841gmail- m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt; On Jan 18, 2017, at 2:59 AM, Francisco Javier Fernández Toro via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt;<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt; Hi,<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt;<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt; I&#39;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&#39;s not enough with ==.<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt;<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt; If you don&#39;t define you own subclass != operator, Swift compiler will use the super class to resolve that operation.<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt;<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
&gt; Is there any reason for that?<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
<br class="m_-4542951819186758422m_4931755600843731047gmail_msg 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="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
<span class="m_-4542951819186758422m_4931755600843731047m_2457570553868603841gmail- m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
class Subclass: Superclass {<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
    let bar: Int<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
    init(foo: Int, bar: Int) {<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
        self.bar = bar<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
        super.init(foo: foo)<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
    }<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
</span>    override func equal(to: Superclass) -&gt; Bool {<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
      if let toSub = to as? Subclass {<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
        return bar == toSub.bar &amp;&amp; super.equal(to: to)<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
      }<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
      return false<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
    }<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
}<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
<br class="m_-4542951819186758422m_4931755600843731047gmail_msg 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="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg" target="_blank">bugs.swift.org</a> about that?<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
<span class="m_-4542951819186758422m_4931755600843731047m_2457570553868603841gmail-HOEnZb m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><font color="#888888" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
-Joe</font></span></blockquote></div><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg"></div></div></div></div></div><span class="gmail_msg">
_______________________________________________<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
swift-evolution mailing list<br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="m_-4542951819186758422m_4931755600843731047gmail_msg gmail_msg">
</span></blockquote></div></div>
</blockquote></div><br class="gmail_msg"></div>
</blockquote></div>