Improve documentation for E0223
This commit is contained in:
parent
c763eceae3
commit
3c4fabc341
@ -3,31 +3,33 @@ An attempt was made to retrieve an associated type, but the type was ambiguous.
|
|||||||
Erroneous code example:
|
Erroneous code example:
|
||||||
|
|
||||||
```compile_fail,E0223
|
```compile_fail,E0223
|
||||||
trait MyTrait {type X; }
|
trait Trait { type X; }
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let foo: MyTrait::X;
|
let foo: Trait::X;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The problem here is that we're attempting to take the type of X from MyTrait.
|
The problem here is that we're attempting to take the associated type of `X`
|
||||||
Unfortunately, the type of X is not defined, because it's only made concrete in
|
from `Trait`. Unfortunately, the type of `X` is not defined, because it's only
|
||||||
implementations of the trait. A working version of this code might look like:
|
made concrete in implementations of the trait. A working version of this code
|
||||||
|
might look like:
|
||||||
|
|
||||||
```
|
```
|
||||||
trait MyTrait {type X; }
|
trait Trait { type X; }
|
||||||
struct MyStruct;
|
|
||||||
|
|
||||||
impl MyTrait for MyStruct {
|
struct Struct;
|
||||||
|
impl Trait for Struct {
|
||||||
type X = u32;
|
type X = u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let foo: <MyStruct as MyTrait>::X;
|
let foo: <Struct as Trait>::X;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This syntax specifies that we want the X type from MyTrait, as made concrete in
|
This syntax specifies that we want the associated type `X` from `Struct`'s
|
||||||
MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct
|
implementation of `Trait`.
|
||||||
might implement two different traits with identically-named associated types.
|
|
||||||
This syntax allows disambiguation between the two.
|
Due to internal limitations of the current compiler implementation we cannot
|
||||||
|
simply use `Struct::X`.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user