wickwirew / Runtime

A Swift Runtime library for viewing type info, and the dynamic getting and setting of properties.
MIT License
1.08k stars 94 forks source link

Instance creation failure when enum property types encountered #90

Open eldcn opened 3 years ago

eldcn commented 3 years ago

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?

wickwirew commented 3 years ago

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.