]>
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 | |
353b0b11 | 6 | trait Trait { type X; } |
60c5eb7d XL |
7 | |
8 | fn main() { | |
353b0b11 | 9 | let foo: Trait::X; |
60c5eb7d XL |
10 | } |
11 | ``` | |
12 | ||
353b0b11 FG |
13 | The problem here is that we're attempting to take the associated type of `X` |
14 | from `Trait`. Unfortunately, the type of `X` is not defined, because it's only | |
15 | made concrete in implementations of the trait. A working version of this code | |
16 | might look like: | |
60c5eb7d XL |
17 | |
18 | ``` | |
353b0b11 | 19 | trait Trait { type X; } |
60c5eb7d | 20 | |
353b0b11 FG |
21 | struct Struct; |
22 | impl Trait for Struct { | |
60c5eb7d XL |
23 | type X = u32; |
24 | } | |
25 | ||
26 | fn main() { | |
353b0b11 | 27 | let foo: <Struct as Trait>::X; |
60c5eb7d XL |
28 | } |
29 | ``` | |
30 | ||
353b0b11 FG |
31 | This syntax specifies that we want the associated type `X` from `Struct`'s |
32 | implementation of `Trait`. | |
33 | ||
34 | Due to internal limitations of the current compiler implementation we cannot | |
35 | simply use `Struct::X`. |