You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
traitA{fnfoo(&self);fnbar(&self);fnbaz(&self);}structBasic{some_field:int}implAforBasic{fnfoo(&self){}fnbar(&self){}fnbaz(&self){}}structExtended{inner:Basic,extra:int}#[delegate_to(inner)]implAforExtended{fnfoo(&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).
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:
implAforBase{fnfoo(&self){if some_condition {self.bar()}}fnbar(&self){}}structExtended{base:Base}#[delegate_to(base)]implAforExtended{}// is equivalent toimplAforExtended{fnfoo(&self){if some_condition {self.bar()}// different to plain self.inner.bar(), which is `if some_condition { self.base.bar() }`}fnbar(&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.)
The text was updated successfully, but these errors were encountered:
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.
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: