If I have a generic struct like...
struct Blah<T> {
let someProperty: T
}
Can I then extend Blah
to conform to Equatable
only when T
is Equatable
. Like...
extension Blah: Equatable where T: Equatable {
static func == (lhs: Blah, rhs: Blah) -> Bool {
return lhs.someProperty == rhs.someProperty
}
}
Is this possible?
I have tried a few different ways of coding this but each gives me a slightly different error.
Update: Conditional conformance has been implemented in Swift 4.1, and your code
struct Blah<T> {
let someProperty: T
}
extension Blah: Equatable where T: Equatable {
static func == (lhs: Blah, rhs: Blah) -> Bool {
return lhs.someProperty == rhs.someProperty
}
}
compiles and works as expected in Xcode 9.3.
What you are looking for is
(which in turn is part of the "Generics Manifesto"). The proposal has been accepted for Swift 4 but not yet implemented. From the proposal:
Conditional conformances express the notion that a generic type will conform to a particular protocol only when its type arguments meet certain requirements.
and a prominent example is
extension Array: Equatable where Element: Equatable {
static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool { ... }
}
to make arrays of equatable elements equatable, which is not possible at present. Your example is essentially
struct SomeWrapper<Wrapped> {
let wrapped: Wrapped
}
extension SomeWrapper: Equatable where Wrapped: Equatable {
static func ==(lhs: SomeWrapper<Wrapped>, rhs: SomeWrapper<Wrapper>) -> Bool {
return lhs.wrapped == rhs.wrapped
}
}
from that proposal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With