protocol ApiRequest {
associatedtype DataType
func execute() -> DataType
}
class SomeApiRequestMock: ApiRequest {
typealias DataType = String
func execute() -> String {
return "1"
}
}
class Simple<D: ApiRequest>{
func creatorMock(_ request: D) -> D.DataType {
request.execute()
}
}
extension Simple where D: SomeApiRequestMock {
var request: SomeApiRequestMock {
return SomeApiRequestMock()
}
var create: D.DataType {
creatorMock(request)
}
}
I have this ApiRequest
protocol and create some concrete class conformed this ApiRequest
protocol.
then I try to use it in some Generic constraint class Simple
. The compiler was happy initially, until I try to call the creatorMock:
func in extensions. and I'll get
I could solve it by adding the same constraint again in method like
func creatorMock<D: ApiRequest>(_ request: D) -> D.DataType
but I don't understand why is that? anyone could help to explain what's going on here?