Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trouble with associatedtype constraints between protocols

I have a protocol:

protocol CellContentViewButtonTappable: CellContentView {
    associatedtype ButtonData
    var delegate: (any CellContentViewButtonTappableDelegate)? { get }
}

and a delegate:

protocol CellContentViewButtonTappableDelegate: AnyObject {
    associatedtype ButtonData
    func buttonTapped(_ data: ButtonData)
}

The point here is that when I call delegate?.buttonTapped(_:) from a class conforming to CellContentViewButtonTappable I want the ButtonData I pass back to be the same as the ButtonData that is in the class conforming to CellContentViewButtonTappableDelegate. But I’m not quite sure how to do this. I’ve tried things like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

protocol CellContentViewButtonTappable: CellContentView {
    associatedtype ButtonData where ButtonData == CellContentViewButtonTappableDelegate.ButtonData
    var delegate: (any CellContentViewButtonTappableDelegate)? { get }
}

but going down this route has always resulted in the error:

Associated type 'ButtonData' can only be used with a concrete type or generic parameter base

I feel like I’m close and this change added in Swift 4 should allow me to do something like this, but I’m just not getting it right. Would be grateful if someone could point me in the right direction.

>Solution :

Rather than associated type constraints in Swift 4, I think you need the primary associated types feature in Swift 5.7 instead.

Declare the delegate like this, with ButtonData as its primary associated type:

protocol CellContentViewButtonTappableDelegate<ButtonData>: AnyObject {
    associatedtype ButtonData
    func buttonTapped(_ data: ButtonData)
}

In CellContentViewButtonTappable, you can then do:

var delegate: (any CellContentViewButtonTappableDelegate<ButtonData>)? { get }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading