How to express type equality constraints on a Generic Associate Type

Suppose we have a base trait with GAT

trait Base {
    type Assoc<T>;
}

How to express the equality constraints described by the following mock code?

trait Derived: Base<for<T> Assoc<T>=Vec<T>> {} // This is mockup syntax

Short answer is you can't do that in Rust.

You may search HKT serials in Rust for niko's blogpost, especially like this one: Associated type constructors, part 3: What higher-kinded types might look like · baby steps

1 Like

I suspected so, just need a confirmation that I was not missing out obvious solutions.

Still as a following note, if anyone like me encountered the above scenario, then most likely what you want can be expressed by the following workaround.

trait Base {
    type HktAssoc: Hkt;
}
trait Hkt {
    type Type<T>;
}
struct HktVec;
impl Hkt for HktVec {
    type Type<T> = Vec<T>;
}
trait Derived: Base<HktAssoc=HktVec> {}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.