You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a class member is replaced after another class has extended from it, the derived class retains the old value. However, if a new member is added, the derived class does see the new value. This is because extend both copies all the class members to the derived class (presumably to avoid the performance cost of having to potentially walk up a deeply nested metatable hierarchy), and also sets the superclass to be the metatable of the derived class.
localFruit=class("Fruit")
functionFruit:print_thing()
print("Original function!")
endlocalApple=Fruit:extend("Apple")
Fruit.print_thing=function()
print("Replaced function!")
endFruit.new_thing=function()
print("A function added after extend!")
endlocala_fruit=Fruit()
localan_apple=Apple()
a_fruit:print_thing() -- Replaced function! [ok]an_apple:print_thing() -- Original function! [bad]an_apple:new_thing() -- A function added after extend! [ok]
I don't have a good solution, so this is more of an FYI that changing classes on the fly (e.g., in a live coding environment) can have unexpected behavior.
The text was updated successfully, but these errors were encountered:
If a class member is replaced after another class has extended from it, the derived class retains the old value. However, if a new member is added, the derived class does see the new value. This is because
extend
both copies all the class members to the derived class (presumably to avoid the performance cost of having to potentially walk up a deeply nested metatable hierarchy), and also sets the superclass to be the metatable of the derived class.I don't have a good solution, so this is more of an FYI that changing classes on the fly (e.g., in a live coding environment) can have unexpected behavior.
The text was updated successfully, but these errors were encountered: