[swift-evolution] Method dispatching issue with subclasses implementing Equatable protocol.

Francisco Javier Fernández Toro fran at gokarumi.com
Wed Jan 18 04:59:14 CST 2017


Hi,

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 ==.

If you don't define you own subclass != operator, Swift compiler will use
the super class to resolve that operation.

Is there any reason for that?

Check the following code (
https://gist.github.com/fjfdeztoro/a5097f1b24379e127674eb1df8c97d96):


class Superclass : Equatable {
    let foo: Int

    init(foo: Int) { self.foo = foo }

    func equal(to: Superclass) -> Bool {
        return foo == to.foo
    }
}

func == (lhs: Superclass, rhs: Superclass) -> Bool {
    return lhs.equal(to: rhs)
}

class Subclass: Superclass {
    let bar: Int
    init(foo: Int, bar: Int) {
        self.bar = bar
        super.init(foo: foo)
    }

    func equal(to: Subclass) -> Bool {
        return bar == to.bar && super.equal(to: to)
    }
}

func == (lhs: Subclass, rhs: Subclass) -> Bool {
    return lhs.equal(to: rhs)
}

class SubclassWithDifferentOperator: Subclass {}

func != (lhs: SubclassWithDifferentOperator, rhs:
SubclassWithDifferentOperator) -> Bool {
    return !lhs.equal(to: rhs)
}

let a = Subclass(foo: 1, bar: 1)
let b = Subclass(foo: 1, bar: 2)

(a == b) != (a != b) // Prints: False

let x = SubclassWithDifferentOperator(foo: 1, bar: 1)
let y = SubclassWithDifferentOperator(foo: 1, bar: 2)

(x == y) != (x != y) // Prints: True

---

Fran Fernandez.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170118/d3e7f000/attachment.html>


More information about the swift-evolution mailing list