07-18-2023, 07:35 PM
It seems that `protocol` doesn't support the `===` operator, while `class` does.
protocol P {
}
class A : P {
}
var a1 = A()
var a2 = A()
var p1:P = a1
var p2:P = a2
a1 === a2 // true
p1 === p2 // error: Type 'P' does not conform to protocol 'AnyObject'
I think it could probably caused by the fact that the concrete type that conforms to the protocol could also be a value type (like `struct`) which doesn't support the `===` operator.
I'm just wondering that if I'm sure the real type is a class, how can I compare the references of them, like the `p1` and `p2` here?
protocol P {
}
class A : P {
}
var a1 = A()
var a2 = A()
var p1:P = a1
var p2:P = a2
a1 === a2 // true
p1 === p2 // error: Type 'P' does not conform to protocol 'AnyObject'
I think it could probably caused by the fact that the concrete type that conforms to the protocol could also be a value type (like `struct`) which doesn't support the `===` operator.
I'm just wondering that if I'm sure the real type is a class, how can I compare the references of them, like the `p1` and `p2` here?