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.
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution