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

RFC: allow delegating some methods from an trait impl to a field of a struct #292

Closed
rust-highfive opened this issue Sep 24, 2014 · 2 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@rust-highfive
Copy link

Issue by huonw
Saturday Jul 13, 2013 at 10:05 GMT

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

This issue was labelled with: A-an-interesting-project, A-attributes, A-traits, B-RFC, I-wishlist in the Rust repository


This would allow one to emulate conventional OO inheritance (to some degree) automatically, instead of requiring a lot of boilerplate, e.g.

trait A {
  fn foo(&self);
  fn bar(&self);
  fn baz(&self);
}

struct Basic {
   some_field: int
}

impl A for Basic {
  fn foo(&self) {}
  fn bar(&self) {}
  fn baz(&self) {}
}

struct Extended {
   inner: Basic,
   extra: int
}

#[delegate_to(inner)]
impl A for Extended {
  fn foo(&self) {} // new version of `foo`
}
/* automatically created:
  fn bar(&self) { self.inner.bar() }
  fn baz(&self) { self.inner.baz() }
*/

This isn't possible as a syntax extension, since the methods in a trait are not known at expansion time. And methods returning Self would have to be implemented by hand.

I guess this is similar to default methods. I think it would allow traits to replace the closures-in-structs pattern entirely (e.g. the ast visitor), since currently it's not easily possible to write Visitor { visit_expr: |e, (a,v)| { ... }, .. some_non_default_visitor } (i.e. replacing only the visit_expr method of a visitor defined elsewhere, which is not the default_visitor()) in terms of default methods only (this came up in my attempt to replace the struct visitors in rustc::middle::lint with @Aatch's trait+default-methods based one).

Related:

A super-wishlist behaviour would be turning any recursive method calls (i.e. calling a method from the same trait) on the base type into calls on the extended type, so:

impl A for Base {
   fn foo(&self) {
       if some_condition { self.bar() }
   }
   fn bar(&self) {}
}

struct Extended { base: Base }

#[delegate_to(base)]
impl A for Extended {}

// is equivalent to

impl A for Extended {
   fn foo(&self) {
       if some_condition { self.bar() }  
       // different to plain self.inner.bar(), which is `if some_condition { self.base.bar() }`
   }
   fn bar(&self) { self.base.bar() }
}

(This is possibly possible by recording "a trait-self" against which to call methods from the same trait, I don't know.)

@burdges
Copy link

burdges commented Jan 18, 2016

Appears to be superseded by #1406

@nrc nrc added the T-lang Relevant to the language team, which will review and decide on the RFC. label Aug 17, 2016
@Centril
Copy link
Contributor

Centril commented Oct 7, 2018

Closing in favor of #2393

@Centril Centril closed this as completed Oct 7, 2018
wycats pushed a commit to wycats/rust-rfcs that referenced this issue Mar 5, 2019
* Setup mdbook

* Change script permissions
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

4 participants