-
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 5 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,75 @@ | ||
# `where` clauses on contextually generic declarations | ||
|
||
* Proposal: [SE-NNNN](NNNN-filename.md) | ||
* Authors: [Anthony Latsis](https://github.com/AnthonyLatsis) | ||
* Review Manager: TBD | ||
* Status: **Ongoing Discussion** | ||
* Implementation: [apple/swift#23489](https://github.com/apple/swift/pull/23489) | ||
|
||
## Introduction | ||
|
||
The objective of this proposal is to lift the restriction on attaching `where` clauses to declarations that themselves | ||
do not carry a generic parameter list explicitly, but inherit the surrounding generic environment. Simply put, this means you no longer have to worry about the `'where' clause cannot be attached` error within generic contexts. | ||
|
||
```swift | ||
struct Box<Wrapped> { | ||
func sequence() -> [Box<Wrapped.Element>] where Wrapped: Sequence { ... } | ||
} | ||
|
||
``` | ||
|
||
> Only declarations that can already be generic and support being constrained via a conditional | ||
> extension are covered by the described enhancement. Properties and new constraint kinds are out | ||
> of scope for this document. For example, 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 can only be expressed indirectly by placing them on conditional | ||
extensions. Unless the constraints are identical, every such declaration requires a separate extension. This apparent | ||
dependence on extensions is an obstacle to stacking up constraints or grouping semantically related APIs and usually | ||
becomes a pain point in heavily generic code that unnecessarily complicates the structure of a program for the developer | ||
and the compiler. | ||
|
||
Leaving ergonomic shortcomings behind, it is only natural for a `where` clause to work anywhere a constraint can be | ||
meaningfully imposed, meaning both of these layout variants should be possible: | ||
```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 move towards «untying» generic parameter lists and `where` clauses is an obvious and farsighted improvement to the generics | ||
system with numerous future applications, including contextually generic computed properties, [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.
Uh oh!
There was an error while loading. Please reload this page.