Instance creation failure when enum property types encountered
eldcn opened this issue · 1 comments
Hey, first of all kudos for the great job with the library.
I do have a question regarding createInstance(of:constructor:)
api. It is currently building struct
and class
kinds. Is there any technical reason why instance creation for enum
is not supported, at least for ones with String
or Int
as RawValue
type? Since the method performs recursive calls on the properties, it fails once a nested enum property is encountered. Are there any plans in the near future to also support instance creation for enums, if this is technically possible of course?
Its definitely possible. The RawValue
actually really doesn't matter and is not stored inline in the enum. So any enum that doesn't have a case with a payload will always be just 1 byte. Which means initializing one is as simple as figuring out the case index and bit casting it as a UInt8
to the enum type.
It gets tricky with cases that have payloads. They are laid out with the payload first, then the case index (most of the time). Single case enums will omit the case part since there is only ever one possible case. Runtime at the moment doesn't get all of the necessary metadata needed to properly initialize one.
I can try to take a look, but if I'm being honest Im pretty busy currently and don't really have a lot of time to look into it.