Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default the associated return type of unop/binop traits to Self #20399

Closed
japaric opened this issue Jan 1, 2015 · 3 comments
Closed

Default the associated return type of unop/binop traits to Self #20399

japaric opened this issue Jan 1, 2015 · 3 comments
Labels
A-traits Area: Trait system C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@japaric
Copy link
Member

japaric commented Jan 1, 2015

Because that's the common case and it's an ergonomic win. For example:

trait Add<RHS=Self> {
   type Result = Self;  // <-- default type

    fn add(self, RHS) -> Self::Result;
}

Would let us write trait Int: Add + Sub + ... instead of trait Int: Add<Result=Self> + Sub<Result=Self> + ....

This has to wait until default type parameters is implemented for associated types (#19476).

cc @aturon / @nikomatsakis

@bluss
Copy link
Member

bluss commented Jan 2, 2015

I don't see how Int: Add puts any restriction on Add::Result, regardless if there is a default value for the assoc type or not.

@kmcallister kmcallister added A-libs A-traits Area: Trait system labels Jan 16, 2015
@Stebalien
Copy link
Contributor

Now blocking on #29661 (stabilization of default associated types).

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Sep 10, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 19, 2017

I agree with @bluss -- I don't see how T: Add would be equivalent to T: Add<Result = T> even if there is a default for the Result associated type. Default associated types as tracked in #29661 allow the associated type to be omitted from an impl of the trait, but does not affect what T: Add means.

trait Add<RHS = Self> {
    type Output;
    fn add(self, RHS) -> Self::Output;
}

struct S;
impl Add for S {
    type Output = ();
    fn add(self, _: S) -> () { unimplemented!() }
}

fn main() {
    fn f<T: Add>() {}
    f::<S>();
}

This compiles on stable Rust today and the function f accepts any type T that implements Add. It would be a breaking change for fn f<T: Add> to change to mean fn f<T: Add<Result = T>> when a default is added.

I agree that the current incantation of Add<Result = Self> + Sub<Result = Self> + ... could be handled better. I would be interested in seeing that explored in an RFC.

@dtolnay dtolnay closed this as completed Nov 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants