I faced this problem for too many times: I need a struct with a generic field that implements a generic trait, then it turns into something like this:
struct MyStruct<A, B, C, B0, B1, C0, C1>
where
A: Foo<B, C>,
B: Bar<B0, B1>,
C: Baz<C0, C1>,
{
field_name: MyType<A>,
}
This leads to a few problems:
- It is hard to type, especially when implementing trait or write a function that involve this struct.
- Order of generic parameters is confusing and hard to remember. Accidentally use the wrong order or forget some parameters would lead to confusing error messages.
- It is easier to make a breaking change on accident.
Questions:
- Is there a way to simplify this?
- If you were to make a proposal to the language (internals.rust-lang.org), what would you propose?
Also, for reference, this is surprisingly simple in TypeScript:
interface MyStruct<T extends Foo<any, any>> {
field_name: MyType<T>
}