简体   繁体   中英

Can a trait have a supertrait that is parameterized by a generic?

Can you do something like this in Rust?

trait A : forall<T> B<T> { ... }

That is, if we want:

impl A for D { ... }

We must first implement:

impl<T> B<T> for D { ... }

No. Rust's type system doesn't currently support any features involving higher kinded types. It does, however, support a similar construction to what you described, but limited to lifetime parameters. For example:

trait B<'a> {}

trait A: for<'a> B<'a> {}

struct D;

impl A for D { }

This is an error:

error[E0277]: the trait bound `for<'a> D: B<'a>` is not satisfied
 --> src/lib.rs:7:6
  |
7 | impl A for D { }
  |      ^ the trait `for<'a> B<'a>` is not implemented for `D`

Until you add the blanket implementation:

impl<'a> B<'a> for D { }

It's not impossible that Rust will eventually add similar functionality for types too, but I wouldn't expect it any time soon.

While the language is unable to specify such a higher kinded constraint, it is often possible to rethink the trait B so that its implementation is universal over this parameter type T .

Considering this example of B :

trait B<T> {
    fn foo(bar: T) -> u32;
}

A type that implements B for any T would be practically equivalent to the type implementing the trait below:

trait UniversalB {
    fn foo<T>(bar: T) -> u32;
}

This one declares the type parameters at method call level. While this trait is no longer object safe, it can still be used as a supertrait:

trait A: UniversalB {}

The implementations may need to be adjusted accordingly.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM