Inspecting a Metatype

Is there any way to discover the features of a type without instantiating an instance of that type? I was hoping Swift's reflection capabilities would allow me to work with my type definitions in a generic way, but I'm beginning to think that this is not possible (yet).

I was hoping to create an array of KeyPaths for a type. Sadly,

Mirror(reflecting: MyClassName.Type)

doesn't yield anything interesting, I guess because a metatype isn't an instance of anything?

1 Like

Swift's current reflection capabilities are indeed limited. In the fullness of time I would like to see a way to access all of the information currently contained in the metatypes... but even that probably wouldn't get what you want to do. What you're probably wanting is something that hasn't been through review yet.

The metatype for a type is actually an instance that exists at runtime. You can read about it here.

Thanks for pointing me to those resources. It's very interesting reading. It would be wonderful if reflection capabilities similar to Java were implemented where you can even get the list of methods on a class. Oh well, back to the drawing board.

Just checking into see if there has been any progress on this? I may be leaning on reflection too heavily (there is always a less dynamic solution), but I would like to be able to enumerate a enum's cases given a metatype, not just a value.

There are third party libraries like Echo that might help you.

Legend, thank you Joe! Hope you're well!