Peter Froehlich wrote:
> What you describe and what Go does is *not* delegation, it's
> *forwarding*. In delegation "self" inside a method that was delegated
> to is the delegating object (the original receiver). In forwarding, "self"
> inside a method that was forwarded to is the receiving object itself
> (the "embedded" object in Go lingo).
Thanks for clearing that up.
Ben Tilly wrote:
> To get the behavior you expect in C++ you'd have to say that the
> method is virtual. This forces a dynamic run-time lookup. You should
> be able to get a similar dynamic run-time lookup in Go with the
> reflect package.
Well, there is a huge difference here - in the C++ case you are
calling the method on the original receiver whether you use the
implementation for the base class or the derived class, while in the
Go case the call has been forwarded to a different (embedded)
instance, so there is no way, reflection or not, to call any methods
on the original receiver.
Changing my test() method to the following makes it print "B" even
though I call it on an instance of D.
func (self B) test() {
t := reflect.Typeof(self);
fmt.Printf("%s\n", t.Name());
}
-Truls