3 ms·
In your view, would this critique also apply to a trait-based version in Rust which supplies a default implementation for `push_all`? https://play.rust-lang.or
by rectang 6y ago
In your view, would this critique also apply to a trait-based version in Rust which supplies a default implementation for `push_all`?
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dfe8e2515be70d6c442705ff22dc883f https://play.rust-lang.org/?version=stable&mode=debug&editio...
trait Stack {
fn push(&mut self, element: i32);
fn push_all(&mut self, all: Vec<i32>) {
for element in all {
self.push(element);
}
}
fn pop(&mut self) -> Option<i32>;
}
As far as I know it's impossible to invoke `super` to get at the default version of `push_all` from an `impl` which overrides `push_all`.
This is still "implementation inheritance", because the implementing type inherits the default implementation of "push_all". But it seems less brittle than classical OOP implementation inheritance.
• No "super" invocations.
• Shallow inheritance hierarchies.
• No direct member variable access from the trait (interface) code.
- valenterry 6y agoYour last point is the relevant here in this context. If the defauklt "push_all" has no access to member variables (how could it - it's defined on the trait) and only calls public methods (such as push) then it is no more powerful than a free function or extension function. Hence it is okay. However, if you where to have another private function "private_push_all" and "push_all" could decide whether to call "push" or "private_push_all" then we would have the same situation again.