Swift5.7 came with primary associated type. I try with these codes, But it shows up with error: Cannot specialize protocol type 'GenericDelegate'
class BaseResponse {}
class ConcreteResponse: BaseResponse {}
protocol GenericDelegate<Response>: AnyObject {
associatedtype Response: BaseResponse
func didGetResponse(response: Response)
}
class BaseCGI<T:BaseResponse> {
weak var delegate: (any GenericDelegate<T>)? //error: Cannot specialize protocol type 'GenericDelegate'
func didGetResponse(response: T) {
self.delegate?.didGetResponse(response: response)
}
}
How can we make an existential type with a specified associated type become a variable?
Use like this:
class FooDelegate: GenericDelegate {
func didGetResponse(response: ConcreteResponse) {
//do something
}
}
let cgi = BaseCGI<ConcreteResponse>()
cgi.delegate = FooDelegate()
cgi.didGetResponse(response: ConcreteResponse())
As I see it you must make your class
BaseCGI
generic over both the response type and the delegate type. Further you need to tell the compiler that the response type for the delegate is the same as the response type for the classThen you create an instance like