1 An attempt was made to constrain an associated type.
3 Erroneous code example:
14 pub trait BoxCar : Box + Vehicle {}
16 fn dent_object<COLOR>(c: dyn BoxCar<Color=COLOR>) {} // Invalid constraint
19 In this example, `BoxCar` has two supertraits: `Vehicle` and `Box`. Both of
20 these traits define an associated type `Color`. `BoxCar` inherits two types
21 with that name from both supertraits. Because of this, we need to use the
22 fully qualified path syntax to refer to the appropriate `Color` associated
23 type, either `<BoxCar as Vehicle>::Color` or `<BoxCar as Box>::Color`, but this
24 syntax is not allowed to be used in a function signature.
26 In order to encode this kind of constraint, a `where` clause and a new type
38 pub trait BoxCar : Box + Vehicle {}
40 // Introduce a new `CAR` type parameter
44 // Bind the type parameter `CAR` to the trait `BoxCar`
46 // Further restrict `<BoxCar as Vehicle>::Color` to be the same as the
47 // type parameter `COLOR`
48 CAR: Vehicle<Color = COLOR>,
49 // We can also simultaneously restrict the other trait's associated type
50 CAR: Box<Color = COLOR>