Say I have an enum case as
Any, and I want to use reflection and unsafe pointers to determine whether this particular case has a payload or not, and access it if it does. I need to be able to determine several things from the runtime:
- How many cases in this enum have payloads at all?
- Is the current case one of the ones with a payload?
- If so, where is that payload?
- How many payloads are in this case, exactly?
Using Echo, I can easily answer the first question. I am not sure about the rest. Echo seems to be a pretty complete representation of the data available to me at runtime—excluding the functions exposed by the runtime—so I am worried that what I'm asking is not possible at all at runtime. I'm hoping someone here can tell me otherwise.
TypeLayout.rst tells me more about how enums are laid out by the compiler, but not how to fetch that layout for any given enum at runtime, such as for enums with multiple values in a single payload, or enums with more than one payload case.
Runtime.md has some interesting funtions related to enum payloads, but I'm not sure any of them will help me.
Edit: it looks like this is something
Mirror is capable of, as shown below, so it should be possible for me to do!
Also, somehow I overlooked
destructiveProjectEnumData in Echo's EnumValueWitnessTable.swift, so I guess it's possible he just hasn't finished this area of the library yet
swift_reflectionMirror_subscript which looks like what I need