The following code will throw an error - "Type 'SomeEnum' does not conform to protocol 'Equatable'" (and Hashable) - even though SomeProtocol conforms to Hashable and Equatable. Why?
One way to fix this is erasing the type to AnySomeProtocol, but I'm curious why is this a compiler error. I'd rather not erase the type when the existential seems to me the most expressive way to go about this.
import Foundation
enum SomeEnum: Hashable, Equatable {
case foo
case associatedFoo(any SomeProtocol)
}
protocol SomeProtocol: Hashable, Equatable {
var field: String { get }
}
struct SomeDataType: Hashable, Equatable {
let field: String
let anotherField: Int
}
extension SomeDataType: SomeProtocol {}
Type erasure won't actually help. Adding to the question how you think it would, would probably rubber duckily answer the question for yourself.
You've got to have the associated types match, in order for them to be
Equatable.You can implement this yourself, given the definition you provided instead, but it won't be synthesized in Swift 5.8 and there's been no talk of it changing that I know of.