Reputation: 1717
protocol A {
associatedtype T
var t: T { get }
}
protocol B: A where T == Int {}
var b: B? = nil // Protocol 'B' can only be used as a generic constraint
// because it has Self or associated type requirements.
Is there a way to create an extended protocol (B) that assigns a concrete type to the base protocol's (A's) associated type, and by doing so "removes" the associated type requirements, and becomes a protocol with no associated types, usable not only as a generic constraint?
Upvotes: 3
Views: 366
Reputation: 422
No, it doesn't seem to work so the associated type always stays inside
protocol A {
associatedtype T
var t: T { get }
}
protocol B: A {
override associatedtype T = Int
}
There are methods to overcome this, for example wrapped classes
class WrappedB: B {
var t: Int
init<S: B>(_ object: S) where S.T == Int {
self.t = object.t
}
}
var b: WrappedB = WrappedB(BB())
Upvotes: 1