-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
So if
<Dog as Pet>::namerefers to the fieldnamein theDogstruct, what does<Cat as Pet>::namerefer to?
Oh, sorry, I misunderstood above. <Dog as Pet>::name is supposed to refer to the Dog::name method.
If you called the field
dogname(in the Dog struct) I think it would be clearer what's going on. Then, presumably, the label would become<Dog as Pet>::dogname.It would make more sense if the
::namerefers to the function (though the function isn't really on heap, presumably and thename: Fidowould then bedogname: Fido.
Yeah, that is what the picture was supposed to show. I think "the heap" as "somewhere in memory". I guess the vtable is generated at compile-time and put somewhere in the binary — and then memory-mapped into the address space when the program is loaded from disk.
Obviously, I'm confused, but I don't think adding this information to the notes would help, because the information still seems inconsistent to me. (Sorry.)
No, I actually think you got it!
I suggest we change the name method to talk or speak in the Pet trait. The implementations can then be something like
impl Pet for Cat {
fn speak(&self) -> String {
String::from("Miau!")
}
}
impl Pet for Dog {
fn speak(&self) -> String {
format!("Woof, my name is {}!", self.name);
}
}That would remove the name ambiguity. We should then also write notes to explain each part of the diagram.
Originally posted by @mgeisler in #1270 (reply in thread)