Your design is really unique, but, unfortunately, I think that you find yourself in an extreme case of the Swift system. In principle, the protocol does not correspond to itself and, as such, your general Decodable.Type is not enough here (i.e. you really need a specific type to satisfy the system requirements of the type). This may explain your error:
Cannot call decode using argument list of type (Decodable.Type, from: Data) . The expected list of type arguments (T.Type, from: Data) .
But, having said that, there really is (dirty!) To crack it. First, create a DecodableWrapper dummy to save your runtime-ish Decodable :
struct DecodableWrapper: Decodable { static var baseType: Decodable.Type! var base: Decodable init(from decoder: Decoder) throws { self.base = try DecodableWrapper.baseType.init(from: decoder) } }
then use it as follows:
DecodableWrapper.baseType = registry["Person"]! let person = try! JSONDecoder().decode(DecodableWrapper.self, from: exampleJSON).base print("person: \(person)")
displays the expected result:
person: Person (first name: "Bob", last name: "Jones")
source share