PDA

View Full Version : “Inheritance” questions



Varsuuk
March 27th, 2018, 21:25
In 5e, template_char there is a windowtitlebar_char template. Up the tree in windowtitlebar there is a widgetTitle variable. In the windowtitlebar there is also a method called setValue(title) which sets windowTitlebar and then calls updatePosition in the same method.

In the 5e derived template, we ensure windowTitlebar exists then do stuff and finally sets windowTitlebar directly and calls updatePosition directly.

Would calling in the derived class instead super.setValue(title) be the equivalent of windowTitlebar.setText(title); onUpdate(); ?

If so, any reason to pick one over the other?
Taking encapsulation into account and using other languages, I’d say the base class better knows what to do with its own members and how to update itself when one changes through its own API. But not being lua or FG conversant yet, not sure if this causes problems not seen in for example, C++?

Varsuuk
March 27th, 2018, 21:29
Moved from another thread since somewhat related. Thanks ;) for all the help so far!

Extra question: Read about super, self, and various other scopes like the controls. Just one thing to be clear, if you have three templates in a hierarchy A,B,C with C being most-derived and A being the root of the hierarchy. Now presume each defines the function foo().

If inside a function B you call
1. super.foo() —> A::foo() is called. (pardon my class scope operator use)
2. self.foo() —> C::foo() is called.
3. foo() —> does this call B::foo()?

If it instead still calls C:foo() (ie, self can be assumed) then how can we call B::foo() within B? ... B.foo()?

Minty23185Fresh
March 27th, 2018, 22:58
regarding A, B and C foo().

To the best of my understanding, the self.foo() always executes if it is available. If not, execution bubbles to super.foo() providing that it exists. If not then on to super.super.foo(). If that doesn't exist you'll get an error. Depending how you handle defining foo() in your templates, you may or may not have access to super and super.super foo. Defining foo can make the code of super and super.super unavailable.

(I've gone to great length to describe a methodology that maintains the existence of and use of foo, super.foo and super.super.foo here (https://www.fantasygrounds.com/forums/showthread.php?42689-Working-With-and-Around-Self-Super-Code-Layering-A-Best-Practice-(-)).)

Varsuuk
March 27th, 2018, 23:55
super = When scripts are layered, then the super variable is defined and can access a script which this object is based on.
self = Always refers to the outermost object with all layers applied, even if called from a lower layer.



Thing is, use if “this.func()” NOT going towards the lead of an inheritance tree (presuming it is defined in that direction) seems counterintuitive to how it is used in some other languages. But of course, if that is so, then it is so - it would not be wrong for Lu’s if that is how it worked, by definition ;)

What I was concerned with, coming from a diffeeent background of non-all-virtual (C++ vs Java etc) is whether accessing a method “in the middle” sees later ?”virtual” overrides or not. Think C++ “slicing” The naming makes me think it probably works more like Java and would indeed head towards the leaf if it was overridden there but until I can test (and probably will not until I finish this first phase) I won’t know.