Mapping a trait over a type-level list as supertraits?


Suppose I’ve got a trait T, with a type parameter R:

trait T<R> {
    // misc

I’d like to have another trait, say TMany, with supertrait T<R> for every R in some list passed to TMany as a type parameter. For instance, if I implement type level lists as

trait Cons<Car, Cdr> {
    type Car;
    type Cdr;

enum Nil {}

I’d like to have TMany defined something like this:

trait TMany<C>: // supertraits go here

such that TMany<Nil> has no supertraits, and if I have

enum A1 {}
impl Cons for A1 {
    type Car = B1;
    type Cdr = Nil;

then TMany<A1> has supertrait B1, and so on as I make longer Cons lists. I am flexible as to whether Cons has associated types or type parameters or is a struct or a trait, but I haven’t figured out how to make this work regardless.

Any suggestions?


I don’t think it’s possible to do type-level programming of the sort you’re hoping for with supertraits. In particular, since there’s no way to conditionally include a supertrait bound, I don’t think you can create base cases for recursion. All supertrait recursion will eventually end in a cycle… and the compiler doesn’t like cycles.

// error[E0391]: unsupported cyclic reference between types/traits detected
trait Boo: Boo {}

What is it that you’re actually trying to do? It’s possible you might be better served by a macro.


Thank you.

Yes, I’ve pretty much come to this conclusion. I tried for a while to find a workaround, but ultimately I’ve decided to throw out this approach and solve my problem without any fancy type-level programming.


This thread reminds me of this interesting blog post: