Closed thomasfanell closed 8 years ago
Looking into this now.
What happens is that the Module load functions call their respective class' load function and both Derived1 and Derived2 (obviously) call their parent's (Base) load function, which resets instanceCount
to zero, since multiple executions are allowed.
Basically this means that any side effect on static member initialization will be evaluated multiple times (as many times as you have sublclasses).
Proposed fix is auto generating a static __done__
variable and using it to only evaluate a class' load function once, like we already do with modules.
On a sidenote, string literals are constructed after class loads which leads to crashes if you try to use one in your static member initialization.
However, I don't think reversing global variable initialization and class load order is wise, since you may initialize a variable that depends on the class' static members.
I will see if there is some way to only reverse the order between (string) literals and class loads.
@thomasfanell
Will be opening a PR for magic-lang in a bit.
Consider this example, where everything works as expected:
Base.ooc
Derived1.ooc
Derived2.ooc
main.ooc
Now, if we instead comment out
d1
andd2
inmain.ooc
and uncomment the creation ofDerived1
andDerived2
in their respective files, we getInstance count = 1
. That is, the static variable is re-initialized for every instance.