Why does an already constrained protocol still have "self or associated type requirements"?

Please see the code below. I don't see how the inheriting protocol is an incomplete type. In the world of C++ this would be a specialization, I guess, making the type non generic.

It would also be great to hear of alternative approaches for what I'm trying to achieve. Thanks!

protocol GenericProtocol {
    associatedtype Collaborator
    func add(_ collaborator: Collaborator)
}

protocol IntProtocol: GenericProtocol where Collaborator == Int {
}

struct IntImpl: IntProtocol {
    func add(_ collaborator: Int) {
    }
}

let specializedRef: IntProtocol = IntImpl() // Protocol 'IntProtocol' can only be used as a generic constraint because it has Self or associated type requirements

Please read this thread, which asked the same question.

Interesting reads, thanks for the amazingly quick reply. So this feature is somewhat supported by the current model, but waiting for an improvement to the compiler?

Yes, mostly.

1 Like
Terms of Service

Privacy Policy

Cookie Policy