Skip to content

Associated types of a subtrait must always be explicit even when a supertrait implementation implies the subtrait with specific associated types #40255

Closed
@jimmycuadra

Description

@jimmycuadra

Imagine the following scenario:

There is a trait A that has many associated types. There are many functions in a program that take a Box<A> only with specific associated types. To avoid having to write out all this boilerplate (including importing all the associated types), I want to create an alias for A with these specific associated types. A type alias cannot work, because you can't do impl Foo for Bar where Foo is a type alias. Let's try subtraits:

trait A {
    type Assoc;
    // Pretend there are several more associated types here to make uses of `Box<A>` inconvenient.
}

trait B: A<Assoc=()> {}

fn takes_b(b: Box<B>) {}

This results in:

error[E0191]: the value of the associated type `Assoc` (from the trait `A`) must be specified
 --> src/lib.rs:8:19
  |
8 | fn takes_b(b: Box<B>) {}
  |                   ^ missing associated type `Assoc` value

This seems like it should work. Note that declaring B this way is valid, as the program compiles if takes_b is commented out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions