-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[DynamicCast] Rework existential casting discussion #33797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DynamicCast] Rework existential casting discussion #33797
Conversation
A previous PR collected the various existential types into a single section of the document. This PR takes advantage of that structure to clarify the discussion of existential and protocol casting: * Adds a general discussion of existential casting * Introduces the "strong existential invariant" and "weak existential invariant" that are common to all existential types * Makes a more precise distinction between protocols and existential types This last point involves a number of careful wording nuances based on the following: * A "protocol" is a syntactic construct defining a collection of requirements and default implementations (the latter via "protocol extensions") * Some protocol definitions (those without associated types or `Self` references) have an associated "protocol witness" existential type * A protocol witness existential type has the same name as the protocol definition from which it is derived but they are conceptually different notions * Existential types such as `Any`, `AnyObject`, and `Error` are not protocols -- for example, they cannot be extended. * `AnyHashable` is a type-erased container type but is not an existential. It does however behave like an existential for casting purposes.
Thanks to @slavapestov for his earlier comment about protocols being things that can be extended. That observation helped me a lot to clarify the language here. |
Caveat: | ||
Protocols that have `associatedtype` properties or which make use of the `Self` typealias cannot be used as independent types. | ||
As such, the discussion below does not apply to them. | ||
Any protocol definition (except those that include an `associatedtype` property or which makes use of the `Self` typealias) has an associated existential type named after the protocol. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@theblixguy This doc should be updated when that constraint is lifted!
Note: The discussion in PR #33735 is also relevant here. |
@swift-ci Please smoke test and merge |
1 similar comment
@swift-ci Please smoke test and merge |
Smoke tests still failing in |
@swift-ci Please smoke test and merge |
A previous PR collected the various existential types into a single section of the document.
This PR takes advantage of that structure to clarify the discussion of existential and protocol casting:
This last point involves a number of careful wording nuances based on the following ideas:
Self
references) have an associated "protocol witness" existential typeAny
,AnyObject
, andError
are not protocols -- for example, they cannot be extended.AnyHashable
is a type-erased container type but is not an existential. It does however behave like an existential for casting purposes (thanks to special handling in both compiler and runtime), so it's described with the true existential types.