This has been discussed back in 2016, and a solution to fill in the type hole with associated types was implemented in SE-0142. However, the following is still illegal:
protocol Identifiable {
associatedtype ID
var id: ID { get }
}
protocol MyProtocol: Identifiable where ID == String {
}
struct MyStruct: MyProtocol {
var id: String {
"..."
}
}
let myExistential: MyProtocol = MyStruct()
Specifically, the last line raises the compiler diagnostic:
error: protocol 'MyProtocol' can only be used as a generic constraint because it has Self or associated type requirements
I'll admit I haven't done too much thinking into the impact of this, but it seems like the where
clause in MyProtocol
removes the associated type requirement, and ID
in MyProtocol
should be treated like a typealias
rather than associatedtype
. So, MyProtocol
should be allowed to be used as an existential protocol. Thoughts?
There is also this similar syntax for defining MyProtocol
that should also fill in the type hole:
protocol MyProtocol: Identifiable {
associatedtype ID = String
}
protocol MyProtocol: Identifiable {
var id: String { get }
}