Skip to content

type parameters of trait functions and impl selection combine in confusing ways #5221

Closed
@Kimundi

Description

@Kimundi

It started with this error:

pub trait VideoDriver {
    static fn init() -> Self;
}

pub trait Platform {
    static fn init<D: VideoDriver>() -> Self;
    static fn foo() -> bool;
}

pub fn run<P: Platform, D: VideoDriver>() {
    let platform: P = Platform::init::<D>();

}
---------------------------------------------------------------------------------------------
trait_generic_method.rs:11:19: 11:38 error: not enough type parameters provided for this item
trait_generic_method.rs:11     let platform: P = Platform::init::<D>();
                                              ^~~~~~~~~~~~~~~~~~~~~~
trait_generic_method.rs:11:19: 11:38 error: cannot determine a type for this bounded type parameter: unconstrained type
trait_generic_method.rs:11     let platform: P = Platform::init::<D>();
                                              ^~~~~~~~~~~~~~~~~~~~~~

Which is confusing because the init() method is just defined on one type parameter.
However if I change the line to let platform: P = Platform::init::<P, D>(); it compiles, which is confusing for the same reasons.

The cause for this is that you can specify the type of an trait impl by appending an ::<T> to the call of an function like this: let is_foo = Platform::foo::<int>(), which is fine as long as the function itself has no specific trait bounds. Combined though it's a mess, as it's not even clear in which order the parameters go.

As the type selection really belongs to the trait, and not to an function of that trait, I propose that the syntax should get changed to let platform = Platform::<P>::init::<D>().
More linenoise, but at least it's not confusing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions