Thanks for the follow-up! Still thinking about the privacy case.
How about if the types don't line up?
Since the compiler is evaluating the trait when it encounters the impl
, it could be able to validate the types (and ops involved). Taking a leaf out of your example:
trait Foo {
fn call(&self) -> &str { &self.x }
}
struct Bar {
x: i32
}
// The compiler can emit type error for the method call()
// expected &str; found i32 for this impl
impl Foo for Bar {}
Similarly, the one below may also fail at the point of impl
trait Foo {
fn by_hundred(&self) -> i32 { self.x / 100 }
}
struct Bar {
x: String
}
// This too will fail with error message about std::ops::Div
// not present for String
impl Foo for Bar {}