Read Types chapter of The Swift language reference
Reading again about ObjectIdentifier
.
In Swift, only class instances and metatypes have unique identities.
Ok, what metatypes is in Swift?
This brings to Types section of Swift language reference.
In Swift, there are two kinds of types: named types and compound types.
A named type is a type that can be given a particular name when it’s defined. Named types include classes, structures, enumerations, and protocols.
A compound type is a type without a name, defined in the Swift language itself. There are two compound types: function types and tuple types. A compound type may contain named types and other compound types.
That is new:
You can put parentheses around a named type or a compound type. However, adding parentheses around a type doesn’t have any effect. For example, (Int) is equivalent to Int.
Have learned something new about tuples (that’s why it is important to read documentation from time to time):
var someTuple = (top: 10, bottom: 12) // someTuple is of type (top: Int, bottom: Int)
someTuple = (top: 4, bottom: 42) // OK: names match
someTuple = (9, 99) // OK: names are inferred <= I have missed that names of tuple elements might be inferred
someTuple = (left: 5, right: 5) // Error: names don't match
This part of Swift always makes me smile:
All tuple types contain two or more types, except for
Void
which is a type alias for the empty tuple type,()
.
And finaly, metatype. Metatype type is a type of a type. SomeClass.self
returns SomeClass
itself, not an instance of SomeClass
. And SomeProtocol.self
returns SomeProtocol
itself, not an instance of a type that conforms to SomeProtocol
at runtime. There’s type(of:)
function to take type as a value from instance of some type. Having metatype a class function of that type could be called. Or instance of that type could be created if there’s required
initialiser class is final
. Strange requirements.