Skip to content

Commit 8da6fbf

Browse files
authored
Merge pull request #35272 from tbkka/tbkka/dynamicCast-existentialMetatypeClarification
2 parents 63abd1a + 0630981 commit 8da6fbf

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

docs/DynamicCasting.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ Casting from a function type F1 to a function type F2 will succeed iff the follo
302302

303303
Note that it is _not_ sufficient for argument and return types to be castable; they must actually be identical.
304304

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+
305307
## Existential Types
306308

307309
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.
372374
It satisfies the weak existential invariant above.
373375

374376
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.
376378

377379
### Protocol Witness types
378380

@@ -491,6 +493,12 @@ Non-protocol types do not have existential metatypes.
491493
For a generic variable `G`, the expression also refers to the regular metatype, even if the generic variable is bound to a protocol.
492494
There is no mechanism in Swift to refer to the existential metatype via a generic variable.)
493495

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).
501+
494502
Example
495503
```
496504
protocol P {

0 commit comments

Comments
 (0)