(Issue discovered in another thread.)
Why the error below? Type
implements Base
and Derived
. Moreover, additional methods are defined for Base
and Derived
. A Derived
trait object is not able to use the additional methods defined for Base
.
trait Base { fn base(&self) -> u32; }
trait Derived: Base {}
struct Type;
impl Base for Type { fn base(&self) -> u32 { 1 } }
impl Derived for Type {}
// Additional methods for everything implementing trait Base.
impl Base {
fn double_base(&self) -> u32 { self.base() * 2 }
}
// Additional methods for everything implementing trait Derived.
impl Derived {
fn triple(&self) -> u32 { self.base() * 3 }
}
fn main() {
let b: Box<Derived> = Box::new(Type);
println!("b.base() = {:?}", b.base());
// error: no method named `double_base` found for type `Derived` in the
// current scope
//println!("b.double_base() = {:?}", (*b).double_base());
println!("b.triple() = {:?}", b.triple());
}
The additional methods implemented on Base
(double_base()
), can only depend on the methods constituting Base
(base()
) which are accessible in the Derived
trait object's vtable. So why not allow them? Is there a fundamental reason, or is this a possible oversight/un-implemented feature?