-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow Self
to appear in the where clause of trait impls
#1647
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,87 @@ | ||
- Feature Name: `allow_self_in_where_clauses` | ||
- Start Date: 2016-06-13 | ||
- RFC PR: (leave this empty) | ||
- Rust Issue: (leave this empty) | ||
|
||
# Summary | ||
[summary]: #summary | ||
|
||
This RFC proposes allowing the `Self` type to be used in every position in trait | ||
implementations, including where clauses and other parameters to the trait being | ||
implemented. | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
`Self` is a useful tool to have to reduce churn when the type changes for | ||
various reasons. One would expect to be able to write | ||
|
||
```rust | ||
impl SomeTrait for MySuperLongType<T, U, V, W, X> where | ||
Self: SomeOtherTrait, | ||
``` | ||
|
||
but this will fail to compile today, forcing you to repeat the type, and adding | ||
one more place that has to change if the type ever changes. | ||
|
||
By this same logic, we would also like to be able to reference associated types | ||
from the traits being implemented. When dealing with generic code, patterns like | ||
this often emerge: | ||
|
||
```rust | ||
trait MyTrait { | ||
type MyType: SomeBound; | ||
} | ||
|
||
impl<T, U, V> MyTrait for SomeStruct<T, U, V> where | ||
SomeOtherStruct<T, U, V>: SomeBound, | ||
{ | ||
type MyType = SomeOtherStruct<T, U, V>; | ||
} | ||
``` | ||
|
||
the only reason the associated type is repeated at all is to restate the bound | ||
on the associated type. It would be nice to reduce some of that duplication. | ||
|
||
# Detailed design | ||
[design]: #detailed-design | ||
|
||
Instead of blocking `Self` from being used in the "header" of a trait impl, | ||
it will be understood to be a reference to the implementation type. For example, | ||
all of these would be valid: | ||
|
||
```rust | ||
impl SomeTrait for SomeType where Self: SomeOtherTrait { } | ||
|
||
impl SomeTrait<Self> for SomeType { } | ||
|
||
impl SomeTrait for SomeType where SomeOtherType<Self>: SomeTrait { } | ||
|
||
impl SomeTrait for SomeType where Self::AssocType: SomeOtherTrait { | ||
AssocType = SomeOtherType; | ||
} | ||
``` | ||
|
||
If the `Self` type is parameterized by `Self`, an error that the type definition | ||
is recursive is thrown, rather than not recognizing self. | ||
|
||
```rust | ||
// The error here is because this would be Vec<Vec<Self>>, Vec<Vec<Vec<Self>>>, ... | ||
impl SomeTrait for Vec<Self> { } | ||
``` | ||
|
||
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
`Self` is always less explicit than the alternative. | ||
|
||
# Alternatives | ||
[alternatives]: #alternatives | ||
|
||
Not implementing this is an alternative, as is accepting Self only in where clauses | ||
and not other positions in the impl header. | ||
|
||
# Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
None |
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.
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.
If you could change this from
SomeOtherStruct<T, U, V>
to<Self as MyTrait>::MyType
, that would make clear the current verbosity is not quite as bad :).