This code does not compile:
protocol A: AnyObject, Equatable {}
class B: A {
static func == (lhs: B, rhs: B) -> Bool {
lhs === rhs
}
}
class C: A {
static func == (lhs: C, rhs: C) -> Bool {
lhs === rhs
}
}
let a = B()
let b = C()
let arr: [any A] = [a, b]
[1, 2, 3].contains(1) // OK
arr.contains(b) // Error:
// Cannot convert value of type 'C' to expected argument type '(any A) throws -> Bool'
arr.contains(b as (any A)) // Same error
the contains(_:)
method is declared in the Array extension where Element is Equatable, all my elements are Equatable, so why this doesn't work?
This question isn't duplicate of this one: Protocol doesn't conform to itself? because the referenced question explains why protocol doesn't conform to itself, and this question asks why the compiler is not able to see the method despite all the requirements had been met.