Skip to content

[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

Merged

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Sep 3, 2020

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 ideas:

  • 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
  • Other 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 special handling in both compiler and runtime), so it's described with the true existential types.

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.
@tbkka
Copy link
Contributor Author

tbkka commented Sep 3, 2020

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.
Copy link
Member

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!

@tbkka
Copy link
Contributor Author

tbkka commented Sep 4, 2020

Note: The discussion in PR #33735 is also relevant here.

@tbkka
Copy link
Contributor Author

tbkka commented Sep 8, 2020

@swift-ci Please smoke test and merge

1 similar comment
@tbkka
Copy link
Contributor Author

tbkka commented Sep 9, 2020

@swift-ci Please smoke test and merge

@tbkka
Copy link
Contributor Author

tbkka commented Sep 9, 2020

Smoke tests still failing in lang/swift/mix_any_object/TestSwiftMixAnyObjectType.py

@tbkka
Copy link
Contributor Author

tbkka commented Sep 9, 2020

@swift-ci Please smoke test and merge

@swift-ci swift-ci merged commit 56f1a55 into swiftlang:master Sep 9, 2020
@tbkka tbkka deleted the tbkka/dynamicCastRework-Specification-d branch August 1, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants