|
6 | 6 | # Summary
|
7 | 7 | [summary]: #summary
|
8 | 8 |
|
9 |
| -This RFC proposes allowing the `Self` type to be used in where clauses for trait |
10 |
| -implementations, as well as referencing associated types for the trait being |
| 9 | +This RFC proposes allowing the `Self` type to be used in every position in trait |
| 10 | +implementations, including where clauses and other parameters to the trait being |
11 | 11 | implemented.
|
12 | 12 |
|
13 | 13 | # Motivation
|
@@ -46,28 +46,40 @@ on the associated type. It would be nice to reduce some of that duplication.
|
46 | 46 | # Detailed design
|
47 | 47 | [design]: #detailed-design
|
48 | 48 |
|
49 |
| -The first half of this RFC is simple. Inside of a where clause for trait |
50 |
| -implementations, `Self` will refer to the type the trait is being implemented |
51 |
| -for. It will have the same value as `Self` being used in the body of the trait |
52 |
| -implementation. |
| 49 | +Instead of blocking `Self` from being used in the "header" of a trait impl, |
| 50 | +it will be understood to be a reference to the implementation type. For example, |
| 51 | +all of these would be valid: |
53 | 52 |
|
54 |
| -Accessing associated types will have the same result as copying the body of the |
55 |
| -associated type into the place where it's being used. That is to say that it |
56 |
| -will assume that all constraints hold, and evaluate to what the type would have |
57 |
| -been in that case. Ideally one should never have to write `<Self as |
58 |
| -CurrentTrait>::SomeType`, but in practice it will likely be required to remove |
59 |
| -issues with recursive evaluation. |
| 53 | +```rust |
| 54 | +impl SomeTrait for SomeType where Self: SomeOtherTrait { } |
| 55 | + |
| 56 | +impl SomeTrait<Self> for SomeType { } |
| 57 | + |
| 58 | +impl SomeTrait for SomeType where SomeOtherType<Self>: SomeTrait { } |
| 59 | + |
| 60 | +impl SomeTrait for SomeType where Self::AssocType: SomeOtherTrait { |
| 61 | + AssocType = SomeOtherType; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +If the `Self` type is parameterized by `Self`, an error that the type definition |
| 66 | +is recursive is thrown, rather than not recognizing self. |
| 67 | + |
| 68 | +```rust |
| 69 | +// The error here is because this would be Vec<Vec<Self>>, Vec<Vec<Vec<Self>>>, ... |
| 70 | +impl SomeTrait for Vec<Self> { } |
| 71 | +``` |
60 | 72 |
|
61 | 73 | # Drawbacks
|
62 | 74 | [drawbacks]: #drawbacks
|
63 | 75 |
|
64 |
| -`Self` is always less explicit than the alternative |
| 76 | +`Self` is always less explicit than the alternative. |
65 | 77 |
|
66 | 78 | # Alternatives
|
67 | 79 | [alternatives]: #alternatives
|
68 | 80 |
|
69 |
| -Not implementing this, or only allowing bare `Self` but not associated types in |
70 |
| -where clauses |
| 81 | +Not implementing this is an alternative, as is accepting Self only in where clauses |
| 82 | +and not other positions in the impl header. |
71 | 83 |
|
72 | 84 | # Unresolved questions
|
73 | 85 | [unresolved]: #unresolved-questions
|
|
0 commit comments