I am using the Thoughtbot Argo structure to parse JSON objects in the model.
I ran into a problem when I have a protocol and its extension as such
protocol SomeProtocol {
associatedtype Model
func foo()
}
extension SomeProtocol where Model: Decodable {
func foo() -> Model? {
guard let model: Model = decode(someJSON) else { return nil }
return model
}
}
and the class corresponding to this protocol looks something like this:
class SomeClass: SomeProtocol {
typealias Model = ArgoModel
func bar() {
print(foo())
}
}
and a model like this
struct ArgoModel {
let id: String
}
extension ArgoModel: Decodable {
static func decode(j: AnyObject) -> Decoded<ArgoModel> {
return curry(self.init)
<^> j <| "id"
}
}
(I also use their Curry library to curry the init method)
The problem I encountered is that in the SomeProtocol extension, the associated Model type cannot be decrypted by Argo. The error I get is this
No 'decode' candidates produced the expected contextual result type 'Self.Model?'
Is this a limitation of a system like Swift? Or am I missing something?
source
share