-
Notifications
You must be signed in to change notification settings - Fork 2.4k
«where» clauses on contextually generic declarations #1011
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
jckarter
merged 14 commits into
swiftlang:master
from
AnthonyLatsis:where-clause-nongeneric
Oct 11, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ea7837b
Create 0000-where-on-contextually-generic.md
AnthonyLatsis 1391db5
Update 0000-where-on-contextually-generic.md
AnthonyLatsis 5d6d9a1
Update 0000-where-on-contextually-generic.md
AnthonyLatsis fb55401
Update 0000-where-on-contextually-generic.md
AnthonyLatsis 6c132c6
Update 0000-where-on-contextually-generic.md
AnthonyLatsis af0a9fb
Update 0000-where-on-contextually-generic.md
AnthonyLatsis df7d32b
Update 0000-where-on-contextually-generic.md
AnthonyLatsis e722244
Update 0000-where-on-contextually-generic.md
AnthonyLatsis ae1260c
Update 0000-where-on-contextually-generic.md
AnthonyLatsis 21e8cef
Update 0000-where-on-contextually-generic.md
AnthonyLatsis ea8a617
Update 0000-where-on-contextually-generic.md
AnthonyLatsis 69c8513
Update 0000-where-on-contextually-generic.md
AnthonyLatsis 099e71b
Update 0000-where-on-contextually-generic.md
AnthonyLatsis 0ac14cc
Update 0000-where-on-contextually-generic.md
AnthonyLatsis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# `where` clauses on contextually generic declarations | ||
|
||
* Proposal: [SE-NNNN](NNNN-filename.md) | ||
* Authors: [Anthony Latsis](https://github.com/AnthonyLatsis) | ||
* Review Manager: TBD | ||
* Status: **Awaiting a Review Manager** | ||
* Implementation: [apple/swift#23489](https://github.com/apple/swift/pull/23489) | ||
|
||
## Introduction | ||
|
||
This proposal aims to lift the mostly artificial restriction on attaching `where` clauses to declarations that reference only outer generic parameters. Simply put, this means you no longer have to worry about the `'where' clause cannot be attached` error inside most generic contexts. | ||
|
||
```swift | ||
struct Box<Wrapped> { | ||
func sequence() -> [Box<Wrapped.Element>] where Wrapped: Sequence { ... } | ||
} | ||
|
||
``` | ||
|
||
> Only declarations that already support genericity and being constrained via a conditional | ||
> extension fall under this enhancement. Properties and hitherto unsupported constraint kinds are out | ||
> of scope for the proposal. For instance, the following remains an error: | ||
> ```swift | ||
> protocol P { | ||
> // error: Instance method requirement 'foo(arg:)' cannot add constraint 'Self: Equatable' on 'Self' | ||
> func foo() where Self: Equatable | ||
> } | ||
> ``` | ||
|
||
Swift-evolution thread: [Discussion thread topic for that proposal](https://forums.swift.org/t/where-clauses-on-contextually-generic-declaractions/22449) | ||
|
||
## Motivation | ||
|
||
Today, `where` clauses on contextually generic declarations are expressed indirectly by placing them inside conditional extensions. Unless constraints are identical, every such declaration requires a separate extension. | ||
This dependence on extensions can be an obstacle to grouping semantically related APIs, stacking up constraints and, | ||
sometimes, the legibility of heavily generic interfaces. | ||
|
||
It is reasonable to expect a `where` clause to work anywhere a constraint can be meaningfully imposed, meaning both of these structuring styles should be available to the user: | ||
|
||
```swift | ||
struct Foo<T> // 'Foo' can be any kind of nominal type declaration. For a protocol, 'T' would be an associatedtype. | ||
|
||
extension Foo where T: Sequence, T.Element: Equatable { | ||
func slowFoo() { ... } | ||
} | ||
extension Foo where T: Sequence, T.Element: Hashable { | ||
func optimizedFoo() { ... } | ||
} | ||
extension Foo where T: Sequence, T.Element == Character { | ||
func specialCaseFoo() { ... } | ||
} | ||
|
||
extension Foo where T: Sequence, T.Element: Equatable { | ||
func slowFoo() { ... } | ||
|
||
func optimizedFoo() where T.Element: Hashable { ... } | ||
|
||
func specialCaseFoo() where T.Element == Character { ... } | ||
} | ||
``` | ||
A step towards generalizing `where` clause usage is an obvious and farsighted improvement to the generics | ||
system with numerous future applications, including [opaque types](https://github.com/apple/swift-evolution/blob/master/proposals/0244-opaque-result-types.md), [generalized | ||
existentials](https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials) and constrained protocol requirements. | ||
|
||
## Source compatibility and ABI stability | ||
|
||
This is an additive change with no impact on the ABI and existing code. | ||
|
||
## Effect on API resilience | ||
|
||
For public declarations in resilient libraries, switching between a constrained extension and a «direct» `where` clause | ||
will not be a source-breaking change, but it most likely will break the ABI due to subtle mangling differences. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.