I want to write two protocols, one generic and one more specific to handle some network requests.
All works fine for the more generic DORequest
protocol, but I canβt get DOPagedRequest
working. The problem is caused by associatedtype Response
, that I want to make more specific in DOPagedRequest
.
Here are my protocols:
public protocol DORequest {
associatedtype Response: DOResponse
var method: String { get }
var path: String { get }
}
public protocol DOResponse: Codable { }
public protocol DOPagedRequest: DORequest where Response: DOPagedResponse {
var page: Int? { get }
var perPage: Int? { get }
}
public protocol DOPagedResponse: Codable {
var links: ResponseLinks { get }
}
public struct ResponseLinks: Codable {
public struct Pages: Codable {
var next: String?
}
public var pages: Pages?
}
And their concrete implementation example:
// Implementation of DORequest with no errors
public struct Get: DORequest {
public struct Response: DOResponse {
public let account: String
}
public let method = "GET"
public let path = "account"
public init() { }
}
// Implementation of DOPagedRequest with errors:
// Type 'List' does not conform to protocol 'DORequest'
// Type 'List' does not conform to protocol 'DOPagedRequest'
public struct List: DOPagedRequest {
public var tag: String?
public var page: Int?
public var perPage: Int?
public struct Response: DOPagedResponse {
public var links: ResponseLinks
public let droplets: [String]
}
public let method = "GET"
public let path = "droplets"
public init(tag: String? = nil, page: Int = 0, perPage: Int = 200) {
self.tag = tag
self.page = page
self.perPage = perPage
}
}
Probably I'm missing something about Swift's associatedtype.
DO
? It looks like an Objective-C prefix. β Jessy