Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclasses retain old members after changes to superclass #29

Open
PyryM opened this issue Jan 10, 2017 · 0 comments
Open

Subclasses retain old members after changes to superclass #29

PyryM opened this issue Jan 10, 2017 · 0 comments

Comments

@PyryM
Copy link

PyryM commented Jan 10, 2017

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.

local Fruit = class("Fruit")
function Fruit:print_thing()
  print("Original function!")
end

local Apple = Fruit:extend("Apple")

Fruit.print_thing = function()
  print("Replaced function!")
end

Fruit.new_thing = function()
  print("A function added after extend!")
end

local a_fruit = Fruit()
local an_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant