You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/DynamicCasting.md
+9-1Lines changed: 9 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -302,6 +302,8 @@ Casting from a function type F1 to a function type F2 will succeed iff the follo
302
302
303
303
Note that it is _not_ sufficient for argument and return types to be castable; they must actually be identical.
304
304
305
+
Caveat: The current Swift compiler supports more general casts of function types in certain cases where the compiler has enough information to statically construct the necessary adapter function. Function types that must be cast at runtime follow the less permissive rules described above.
306
+
305
307
## Existential Types
306
308
307
309
Conceptually, an "existential type" is an opaque wrapper that carries a type and an instance of that type.
@@ -372,7 +374,7 @@ For casting purposes, `AnyHashable` behaves like an existential type.
372
374
It satisfies the weak existential invariant above.
373
375
374
376
However, note that `AnyHashable` does not act like an existential for other purposes.
375
-
For example, it's metatype is named `AnyHashable.Type` and it does not have an existential metatype.
377
+
For example, its metatype is named `AnyHashable.Type` and it does not have an existential metatype.
376
378
377
379
### Protocol Witness types
378
380
@@ -491,6 +493,12 @@ Non-protocol types do not have existential metatypes.
491
493
For a generic variable `G`, the expression also refers to the regular metatype, even if the generic variable is bound to a protocol.
492
494
There is no mechanism in Swift to refer to the existential metatype via a generic variable.)
493
495
496
+
In essence, an existential metatype simply defines an existential that can hold a metatype instance.
497
+
An instance of the existential metatype `Any.Type` can hold any metatype instance.
498
+
An instance of the existential metatype `P.Type` of a protocol `P` can hold a metatype instance of any type that conforms to `P`.
499
+
As with other existentials, casting _from_ an existential metatype is equivalent to casting the contents of the existential.
500
+
Casting _to_ an existential metatype succeeds whenever the source is a conforming metatype instance (or can be unwrapped to yield such a metatype instance).
0 commit comments