Skip to content

Parametrized protocol types #40714

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
merged 16 commits into from
Jan 26, 2022

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Dec 30, 2021

This allows you to write constructs like Sequence<Int> in generic requirement-like contexts:

  • Inheritance clauses of protocols, generic parameters, associated types
  • Conformance requirements in where clauses
  • Extensions
  • Opaque result types

With the first three, the new syntax is just sugar. With opaque result types though, it gives you the ability to express something that could not previously be expressed.

The protocol must declare a primary associated type (for now, using the @_primaryAssociatedType attribute; this part isn't final). Assuming that the primary associated type of Sequence is Element, the desugaring is that T : Sequence<Int> is equivalent to T : Sequence where T.Element == Int.

Note that currently, neither Sequence nor any other standard library type actually use this new attribute. You can only use it with your own protocols for now.

This is all gated by the -enable-parametrized-protocol-types flag, since this is an unofficial feature. I'll send out an evolution pitch soon.

@slavapestov slavapestov force-pushed the parametrized-protocol-type branch from f158c70 to 92cc0c9 Compare January 26, 2022 05:03
@slavapestov slavapestov marked this pull request as ready for review January 26, 2022 05:04
@slavapestov slavapestov requested a review from hborla January 26, 2022 05:06
@slavapestov slavapestov force-pushed the parametrized-protocol-type branch 2 times, most recently from 1a97cb6 to e0a7999 Compare January 26, 2022 05:15
@slavapestov
Copy link
Contributor Author

@DougGregor I'm curious if this Just Works with #40993...

@slavapestov slavapestov force-pushed the parametrized-protocol-type branch from e0a7999 to 06e58d2 Compare January 26, 2022 06:12
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

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.

1 participant