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

Allow a minimum complete implementation to be specified for mutually recursive default methods #628

Open
steveklabnik opened this issue Jan 21, 2015 · 1 comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@steveklabnik
Copy link
Member

Issue by huonw
Saturday Jul 13, 2013 at 08:41 GMT

For earlier discussion, see rust-lang/rust#7771

This issue was labelled with: A-attributes, A-diagnostics, A-lint, A-traits, B-RFC, I-wishlist in the Rust repository


It should be possible to tell the compiler that at least one of a given set of default methods is required to be implemented, e.g.

#[requires(one_of(foo, bar), one_of(baz, qux))]
trait A {
   fn foo(&self) { self.bar() }
   fn bar(&self) { self.foo() }

   fn baz(&self) { self.qux() }
   fn qux(&self) { self.baz() }
}

impl A for int { // ok
   fn foo(&self) {}
   fn bar(&self) {}

   fn baz(&self) {} 
}

impl A for uint { // "requires at least one of foo or bar"
   fn baz(&self) {}
}

impl A for float { // "requires at least one of baz or qux"
   fn foo(&self) {}
}

This allows e.g. Eq to write eq and ne in terms of each other, without allowing the infinitely recursive impl Eq for Foo {}.

It could even allow saying "either foo or both bar and baz", e.g. #[requires(one_of(foo, all_of(bar, baz)))], and would hopefully warn if a non-default method was listed.

@Pzixel
Copy link

Pzixel commented Oct 3, 2023

I think I have an example of similar issue which maybe can be narrowed to this:

trait Foo<A, B = A> {
    fn get_a() -> A;

    fn get_b() -> B {
        Self::get_a()
    }
}

So an idea here that I want get_b to have a default implementation if B = A or require it otherwise. ideally it should work for associated types as well:

trait Foo {
    type A;
    fn get_a() -> Self::A;

    type B = A;

    fn get_b() -> Self::B {
        Self::get_a()
    }
}

But I'm not sure if it's possible to allow it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

3 participants