You can't cast AnyObject
to an Equatable
.
What you can do is define Item
as a generic for which the value
, Wrapped
, must be Equatable
:
class Item<Wrapped: Equatable> {
var title: String
var value: Wrapped
init(title: String, value: Wrapped) {
self.title = title
self.value = value
}
}
extension Item: Equatable {
static func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.title == rhs.title && lhs.value == rhs.value
}
}
And, let's imagine that you have some class, Foo
, that (a) isn't equatable; (b) is something you want to wrap in an Item
; and (c) you really want to define them to be equatable on the basis of the identity operator, ===
. (I confess, I find that notion, which you call "soft equatable" fairly disturbing notion, but I won't go into that here.)
Anyway, you can just make your class Foo
equatable on the basis of the identity operator:
extension Foo: Equatable {
static func ==(lhs: Foo, rhs: Foo) -> Bool {
return lhs === rhs
}
}
Or, if you need to do this for many classes, you could even have a protocol for this identity-equality, and then your non-equatable classes could just conform to that:
protocol IdentityEquatable: class, Equatable { }
extension IdentityEquatable {
static func ==(lhs: Self, rhs: Self) -> Bool {
return lhs === rhs
}
}
Then any classes that you want to wrap in an Item
that aren't Equatable
could adopt this identity-equatable behavior with a single line of code each:
extension Foo: IdentityEquatable { }
extension Bar: IdentityEquatable { }
extension Baz: IdentityEquatable { }
As an aside, SE-0143 has been approved and while not part of the language yet, offers the promise of Conditional Conformance in future Swift versions, namely:
class Item<Wrapped> {
var title: String
var value: Wrapped
init(title: String, value: Wrapped) {
self.title = title
self.value = value
}
}
extension Item: Equatable where Wrapped: Equatable {
static func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.title == rhs.title && lhs.value == rhs.value
}
}
In this case, Item
would be Equatable
if and only if the Wrapped
value was Equatable
. This isn't part of the language yet, but looks like it will be in a future version. It is an elegant solution to this problem (though not, admittedly, your "soft equatable" idea).