When you call decode with type, the inferred generic parameter is A, which doesn't conform to Decodable because protocols do not conform to other protocols.
You may also want to wrap such code with a generic context to avoid passing protocols to generic functions with conformance requirements.
For example,
protocol A : Codable {
init()
var json: Data { get }
func decode()
}
extension A {
var json: Data {
get { return try! JSONEncoder().encode(self) }
}
func decode() {
try! JSONDecoder().decode(Self.self, from: Self().json)
}
}
class B : A {
required init() {}
var prop = "some stuff"
}
@anthonylatsis I am not sure I get the wrapping, since it seems to me that it takes away the benefit of the automatic synthesized "stuff" that comes with the Codable protocol.
As long as the relevant type can conform to Codable, the conformance will be synthesized.
Assuming protocol A is supposed to be used with a family of codable entities, I generalized decoding over anything that conforms to A using a convenience function. This way you don't have to worry about type safety or calling through to JSONEncoder and passing the already derived arguments. On the other hand, the helper function could of course be declared on B for an isolated case. For instance, when another class conforming to A uses a different coding strategy.