I have a struct that contains an error property typed as Error:
struct Foo {
var error: Error
}
I would like Foo to be Equatable and I would like to get the synthesized equatable implementation, since there are many properties on Foo. The error property is an obvious stumbling block, though. I don’t mind comparing error equality using localizedDescription, but is there a way to do it while keeping the synthesized Equatable?
The only solution I have come up with is a wrapper:
public struct AnyError {
public let wrapped: Error
public init(_ wrapped: Error) {
self.wrapped = wrapped
}
}
extension AnyError: Equatable {
public static func == (lhs: AnyError, rhs: AnyError) -> Bool {
return lhs.wrapped.localizedDescription == rhs.wrapped.localizedDescription
}
}
struct Foo {
var error: AnyError
}
Is there a better solution?