Swift 5.1
Suppose I'm deserializing some data. I'm interested in reconstructing the exact same types as were serialized, but some may involve generics. Consider the following example case.
let layers = 4
let value: Int64 = 17
var output: Any
???SOMETHING???
print("\(type(of: output))") // "Optional<Optional<Optional<Optional<Int64>>>>"
Is there a way to get an output
with a runtime type of Optional<...Optional<Int64>...>
, where the number of nested layers of Optional
is equal to layers
? (And doesn't rely on e.g. hard-coded case statements full of classes.) Perhaps reflection, some type initializers I don't know about, maybe some black magic use of pointers or object_setClass
or something?
I would also accept strong evidence that it is fundamentally impossible in Swift, such as documentation stating that every specification of a generic class used in a program is generated individually at compiletime, and therefore you cannot newly specify a generic class at runtime. ...Though even in that case, it might still be possible, considering things like Java's ClassLoader
s.
Aucun commentaire:
Enregistrer un commentaire