Metatype
Last updated
Last updated
A metatype type refers to the type of any type, including class types, structure types, enumeration types, and protocol types.
The dynamic type (runtime type) returned from type(of:) is a concrete metatype (T.Type) for a class, struct, enum, or other nonprotocol type T
, or an existential metatype (P.Type) for a protocol or protocol composition P
.
When the static type (compile-time type) of the value passed to type(of:) is constrained to a class or protocol, you can use that metatype to access initializers or other static members of the class or protocol.
Use an initializer expression to construct an instance of a type from that typeโs metatype value.
For class instances, the initializer thatโs called must be marked with required or the entire class marked with final. (the compiler must make sure the initializer is implemented by every possible subtype of the type)