I think Rust needs some sort of limited inheritance.
Yes, I'm bracing for impact after posting a title like that haha.
Perhaps 'inheritance' is the wrong word here and a more accurate statement would be something like 'trait implementation delegation'
I was recently building a project on top of a very modular library that's almost entirely built using 100s of traits which is awesome! My goal was to take an existing implementation of this library, and only slightly modify a couple of its trait implementations.
Suppose we have traits A, B, C, .... And there is another crate with a primary type we'll call Other which implements all of those traits. Now I need to create a new type MyType(Other) which behaves exactly the same as Other except of its implementation of A.
I've now got to implements all N traits for MyType even though they all just default to the inner type. This sounds like not the end of the world, but in this particular example it was literally over 1000 lines of code to implement all the traits.
Perhaps some new syntax is warranted for types like this which automatically delegate all trait implementations to the inner type except for methods that are explicitly overridden. Something like this maybe
// This is in the upstream crate
trait DoSomethingCool: A + B + C {
fn do_something_cool(&self) { .. }
}
// If a trait is not explicitly defined for MyType, The implementation
// for 'Other' will be used
stuct MyType(delegate Other)
// Here's how it could look with named fields
stuct MyTypeAlt {
pub delegate inner: Other,
pub other_field: String,
}
// This overrides the implementation of A for Other
impl A for MyType {
...
}
// now we can use our new type like this
let my_struct = MyStruct(other);
my_struct.do_something_cool();