Hi, i'm having some trouble understanding how method overriding works.
For context i'm making a ui library.
I have a WidgetInterface with some methods to be defined on all widgets
type WidgetInterface interface {
GetWidth() int
SetWidth(value int)
GetMaxWidth() int
SetMaxWidth(value int)
GetMaxInnerWidth() int
// ...
}
and a base struct Widget with some default implementations
type Widget struct {
parent WidgetInterface
width int
maxWidth int
// ...
}
func (w *Widget) GetMaxWidth() int {
if w.parent != nil && w.parent.GetMaxInnerWidth() < w.maxWidth {
return w.parent.GetMaxInnerWidth()
}
return w.maxWidth
}
func (w *Widget) GetMaxInnerWidth() int {
return w.GetMaxWidth()
}
I then have other structs for each type of widget. In the case of the ContainerWidget, i want the GetMaxInnerWidth to include padding.
type ContainerWidget struct {
*Widget
paddingLeft int
paddingRight int
// ...
}
func (w *ContainerWidget) GetMaxInnerWidth() int {
return w.GetMaxWidth() - (w.paddingLeft + w.paddingRight)
}
However, when I go to call to calling w.GetMaxWidth() from another widget TextWidget, it does not seem to use the overriden version method, instead using the one defined in Widget.
Expected:
On a TextWidget tw
tw.GetMaxWidth() is called
tw.parent.GetMaxInnerWidth() where GetMaxInnerWidth comes from ContainerWidget
What actually seems to happen:
On a TextWidget tw
tw.GetMaxWidth() is called
tw.parent.GetMaxInnerWidth() where GetMaxInnerWidth comes from Widget
Method overriding seems to work for other methods so it confuses me a bit as to why it doesn't work here. Any ideas?
ok so