]>
Commit | Line | Data |
---|---|---|
60c5eb7d | 1 | An attempt was made to retrieve an associated type, but the type was ambiguous. |
dfeec247 XL |
2 | |
3 | Erroneous code example: | |
60c5eb7d XL |
4 | |
5 | ```compile_fail,E0223 | |
6 | trait MyTrait {type X; } | |
7 | ||
8 | fn main() { | |
9 | let foo: MyTrait::X; | |
10 | } | |
11 | ``` | |
12 | ||
13 | The problem here is that we're attempting to take the type of X from MyTrait. | |
14 | Unfortunately, the type of X is not defined, because it's only made concrete in | |
15 | implementations of the trait. A working version of this code might look like: | |
16 | ||
17 | ``` | |
18 | trait MyTrait {type X; } | |
19 | struct MyStruct; | |
20 | ||
21 | impl MyTrait for MyStruct { | |
22 | type X = u32; | |
23 | } | |
24 | ||
25 | fn main() { | |
26 | let foo: <MyStruct as MyTrait>::X; | |
27 | } | |
28 | ``` | |
29 | ||
30 | This syntax specifies that we want the X type from MyTrait, as made concrete in | |
31 | MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct | |
32 | might implement two different traits with identically-named associated types. | |
33 | This syntax allows disambiguation between the two. |