-
Notifications
You must be signed in to change notification settings - Fork 13.3k
RFC: allow delegating some methods from an trait impl to a field of a struct #7773
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
Comments
It would be pretty neat to have something like this in Rust. It would also make it easier to write a wrapper for c++ code. |
+1 i'm interested in this - only single inheritance , its simple from a low level perspective. used to do something like "single inheritance" in asm where you simply use the same struct offsets from object pointers. it looks to me like the compiler source has cases that would be simplified out by single inheritance, i.e. a lot of AST nodes with some common information. composition is generally superior,but that doesn't mean inheritance is useless. Is this simple or would implementing this create complex interactions with traits / trait objects? is single inheritance against the design principles of rust? |
@dobkeratops note that this issue isn't about struct inheritance (i.e. "copying" fields of one struct into another) so much as reducing the boilerplate relating to having a field implementing a certain trait and wanting to reuse this implementation for the overall object. (i.e. one still has the "base" structs as explicit fields.) |
ok . I guess i'm accidently hijacking here.. related but not identical.. |
This may have already been brought up already, but one thing I found interesting about Plan 9's flavor of C (which I believe was adopted in Go as well) was embedding structs in other structs:
I'm not entirely sure that this would work in rust because shadowing causes real issues, but I always thought it was kinda cool. |
Visiting for triage. Nothing to add here other than this sounds like a good idea and something that could be implemented post 1.0 |
This issue has been moved to the RFCs repo: rust-lang/rfcs#292 |
Move module declarations back into lib.rs With rust-lang#7673 we moved a lot of things from lib.rs to lib.foo.rs. Unfortunately, rustfmt doesn't seem to work when module declarations are included via `include!` (and trying the `mod foo; use foo::*;` trick doesn't seem to work much either in our specific case). With this PR we continue generating everything in subfiles except for module declarations, which are now generated within lib.rs. changelog: none
This would allow one to emulate conventional OO inheritance (to some degree) automatically, instead of requiring a lot of boilerplate, e.g.
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 thevisit_expr
method of a visitor defined elsewhere, which is not thedefault_visitor()
) in terms of default methods only (this came up in my attempt to replace the struct visitors inrustc::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:
(This is possibly possible by recording "a trait-self" against which to call methods from the same trait, I don't know.)
The text was updated successfully, but these errors were encountered: