Swift Equatable on a protocol

39,488

Solution 1

If you directly implement Equatable on a protocol, it will not longer be usable as a type, which defeats the purpose of using a protocol. Even if you just implement == functions on protocols without Equatable conformance, results can be erroneous. See this post on my blog for a demonstration of these issues:

https://khawerkhaliq.com/blog/swift-protocols-equatable-part-one/

The approach that I have found to work best is to use type erasure. This allows making == comparisons for protocol types (wrapped in type erasers). It is important to note that while we continue to work at the protocol level, the actual == comparisons are delegated to the underlying concrete types to ensure correct results.

I have built a type eraser using your brief example and added some test code at the end. I have added a constant of type String to the protocol and created two conforming types (structs are the easiest for demonstration purposes) to be able to test the various scenarios.

For a detailed explanation of the type erasure methodology used, check out part two of the above blog post:

https://khawerkhaliq.com/blog/swift-protocols-equatable-part-two/

The code below should support the equality comparison that you wanted to implement. You just have to wrap the protocol type in a type eraser instance.

protocol X {
    var name: String { get }
    func isEqualTo(_ other: X) -> Bool
    func asEquatable() -> AnyEquatableX
}

extension X where Self: Equatable {
    func isEqualTo(_ other: X) -> Bool {
        guard let otherX = other as? Self else { return false }
        return self == otherX
    }
    func asEquatable() -> AnyEquatableX {
        return AnyEquatableX(self)
    }
}

struct Y: X, Equatable {
    let name: String
    static func ==(lhs: Y, rhs: Y) -> Bool {
        return lhs.name == rhs.name
    }
}

struct Z: X, Equatable {
    let name: String
    static func ==(lhs: Z, rhs: Z) -> Bool {
        return lhs.name == rhs.name
    }
}

struct AnyEquatableX: X, Equatable {
    var name: String { return value.name }
    init(_ value: X) { self.value = value }
    private let value: X
    static func ==(lhs: AnyEquatableX, rhs: AnyEquatableX) -> Bool {
        return lhs.value.isEqualTo(rhs.value)
    }
}

// instances typed as the protocol
let y: X = Y(name: "My name")
let z: X = Z(name: "My name")
let equalY: X = Y(name: "My name")
let unequalY: X = Y(name: "Your name")

// equality tests
print(y.asEquatable() == z.asEquatable())           // prints false
print(y.asEquatable() == equalY.asEquatable())      // prints true
print(y.asEquatable() == unequalY.asEquatable())    // prints false

Note that since the type eraser conforms to the protocol, you can use instances of the type eraser anywhere an instance of the protocol type is expected.

Hope this helps.

Solution 2

The reason why you should think twice about having a protocol conform to Equatable is that in many cases it just doesn't make sense. Consider this example:

protocol Pet: Equatable {
  var age: Int { get }
}

extension Pet {
  static func == (lhs: Pet, rhs: Pet) -> Bool {
    return lhs.age == rhs.age
  }
}

struct Dog: Pet {
  let age: Int
  let favoriteFood: String
}

struct Cat: Pet {
  let age: Int
  let favoriteLitter: String
}

let rover: Pet = Dog(age: "1", favoriteFood: "Pizza")
let simba: Pet = Cat(age: "1", favoriteLitter: "Purina")

if rover == simba {
  print("Should this be true??")
}

You allude to type checking within the implementation of == but the problem is that you have no information about either of the types beyond them being Pets and you don't know all the things that might be a Pet (maybe you will add a Bird and Rabbit later). If you really need this, another approach can be modeling how languages like C# implement equality, by doing something like:

protocol IsEqual {
  func isEqualTo(_ object: Any) -> Bool
}

protocol Pet: IsEqual {
  var age: Int { get }
}

struct Dog: Pet {
  let age: Int
  let favoriteFood: String

  func isEqualTo(_ object: Any) -> Bool {
    guard let otherDog = object as? Dog else { return false }

    return age == otherDog.age && favoriteFood == otherDog.favoriteFood
  }
}

struct Cat: Pet {
  let age: Int
  let favoriteLitter: String

  func isEqualTo(_ object: Any) -> Bool {
    guard let otherCat = object as? Cat else { return false }

    return age == otherCat.age && favoriteLitter == otherCat.favoriteLitter
  }
}

let rover: Pet = Dog(age: "1", favoriteFood: "Pizza")
let simba: Pet = Cat(age: "1", favoriteLitter: "Purina")

if !rover.isEqualTo(simba) {
  print("That's more like it.")
}

At which point if you really wanted, you could implement == without implementing Equatable:

static func == (lhs: IsEqual, rhs: IsEqual) -> Bool { return lhs.isEqualTo(rhs) }

One thing you would have to watch out for in this case is inheritance though. Because you could downcast an inheriting type and erase the information that might make isEqualTo not make logical sense.

The best way to go though is to only implement equality on the class/struct themselves and use another mechanism for type checking.

Solution 3

Determining equality across conformances to a Swift protocol is possible without type erasure if:

  • you are willing to forgo the operator syntax (i.e. call isEqual(to:) instead of ==)
  • you control the protocol (so you can add an isEqual(to:) func to it)
import XCTest

protocol Shape {
    func isEqual (to: Shape) -> Bool
}

extension Shape where Self : Equatable {
    func isEqual (to: Shape) -> Bool {
        return (to as? Self).flatMap({ $0 == self }) ?? false
    }
}

struct Circle : Shape, Equatable {
    let radius: Double
}

struct Square : Shape, Equatable {
    let edge: Double
}

class ProtocolConformanceEquality: XCTestCase {

    func test() {
        // Does the right thing for same type
        XCTAssertTrue(Circle(radius: 1).isEqual(to: Circle(radius: 1)))
        XCTAssertFalse(Circle(radius: 1).isEqual(to: Circle(radius: 2)))

        // Does the right thing for different types
        XCTAssertFalse(Square(edge: 1).isEqual(to: Circle(radius: 1)))
    }

}

Any conformances don't conform to Equatable will need to implement isEqual(to:) themselves

Solution 4

maybe this will be helpful for you:

protocol X:Equatable {
    var name: String {get set}

}

extension X {
    static func ==(lhs: Self, rhs: Self) -> Bool {
        return lhs.name == rhs.name
    }
}

struct Test : X {
    var name: String
}

let first = Test(name: "Test1")
let second = Test(name: "Test2")

print(first == second) // false

Solution 5

All people who say that you can't implement Equatable for a protocol just don't try hard enough. Here is the solution (Swift 4.1) for your protocol X example:

protocol X: Equatable {
    var something: Int { get }
}

// Define this operator in the global scope!
func ==<L: X, R: X>(l: L, r: R) -> Bool {
    return l.something == r.something
}

And it works!

class Y: X {
    var something: Int = 14
}

struct Z: X {
    let something: Int = 9
}

let y = Y()
let z = Z()
print(y == z) // false

y.something = z.something
print(y == z) // true

The only problem is that you can't write let a: X = Y() because of "Protocol can only be used as a generic constraint" error.

Share:
39,488
drekka
Author by

drekka

Old school. Been around since the days of the Big Iron 3090s. Programmed everything from mainframes to pocket calculators. I go everywhere on motorcycles and am constantly hassled by my cats for somewhere to sleep.

Updated on November 11, 2021

Comments

  • drekka
    drekka over 2 years

    I don't think this can be done but I'll ask anyway. I have a protocol:

    protocol X {}
    

    And a class:

    class Y:X {}
    

    In the rest of my code I refer to everything using the protocol X. In that code I would like to be able to do something like:

    let a:X = ...
    let b:X = ...
    if a == b {...}
    

    The problem is that if I try to implement Equatable:

    protocol X: Equatable {}
    func ==(lhs:X, rhs:X) -> Bool {
        if let l = lhs as? Y, let r = hrs as? Y {
            return l.something == r.something
        }
        return false
    } 
    

    The idea to try and allow the use of == whilst hiding the implementations behind the protocol.

    Swift doesn't like this though because Equatable has Self references and it will no longer allow me to use it as a type. Only as a generic argument.

    So has anyone found a way to apply an operator to a protocol without the protocol becoming unusable as a type?

  • drekka
    drekka over 7 years
    Thanks, but this works until you try to do something like `let first = Test(name: "Test1") as X' then you get the error about not allowing 'X' as a type. My problem is that my classes are deep within an API and only expose themselves via the protocol. So the usage of those instances must be of the protocol type.
  • drekka
    drekka over 7 years
    Thanks. I"m trying to avoid constraining a protocol extension to a class because (and perhaps this is a bad idea but it works for my case) I have different implementations of the protocol which in effect refer to the same thing, therefore I would like them to be regarded as equal.
  • drekka
    drekka over 7 years
    Thanks. I've been considering the isEqual option (from Java) but was hoping to keep it simple because I have a case where I have different classes that represent the same contextural thing and therefore I would them to be regarded as equal even though they are different implementations.
  • Scott H
    Scott H over 7 years
    Implementing equality on two different types of objects is a slippery slope to get on. I'd recommend another option to compare them, perhaps by transforming one type into the other one and then comparing them.
  • adib
    adib over 4 years
    This is the best answer as of Swift 5.1 – would be better if provided with another example such that isEqual(to:) would provide a default implementation to other protocols other than Shape
  • aeskreis
    aeskreis over 3 years
    This should have more upvotes, solid solution
  • Lou Zell
    Lou Zell almost 3 years
    That's pretty cool. One gotcha to point out is that it doesn't compare types, by design. So if you were to have struct Square: Shape { let sides = 4 } and struct Rhombus: Shape { let sides = 4 } then Square() == Rhombus() is true. Perhaps an easy fix to also check the type is to change the return from the == operator to be: return (r as? L)?.something == r.something
  • kelin
    kelin almost 3 years
    @LouZell, interesting. Did you check if r as? L works?
  • Lou Zell
    Lou Zell over 2 years
    Yup! It worked for my limited case (where I need to differentiate between two conformers of a protocol, e.g. Rhombus != Square in the example above)
  • Rizwan Ahmed
    Rizwan Ahmed over 2 years
    This is the solid solution. It definitely must have more upvotes