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

Static inheritance seems broken #67

Open
13k opened this issue Jul 6, 2023 · 0 comments
Open

Static inheritance seems broken #67

13k opened this issue Jul 6, 2023 · 0 comments

Comments

@13k
Copy link

13k commented Jul 6, 2023

Given the file static.lua:

local class = require("middleclass")

local C = class("C")

function C.static:foo()
  return "<static> C.static:foo"
end

function C:initialize()
  self.value = self.class:foo()
end

function C:foo()
  return "<instance> C:foo"
end

local c = C()

print(("[%s] C.static.foo"):format(C.static.foo))
print(("[%s] C.foo"):format(C.foo))
print(("[%s] c.foo"):format(c.foo))
print(("C:foo() = %q"):format(C:foo()))
print(("c:foo() = %q"):format(c:foo()))
print(("c.value = %q"):format(c.value))

local D = class("D", C)

local d = D()

print("---")
print(("[%s] D.static.foo"):format(D.static.foo))
print(("[%s] D.foo"):format(D.foo))
print(("[%s] d.foo"):format(d.foo))
print(("D:foo() = %q"):format(D:foo()))
print(("d:foo() = %q"):format(d:foo()))
print(("d.value = %q"):format(d.value))

And its output:

> lua static.lua 
[function: 0x40cf4998] C.static.foo
[function: 0x40cf4998] C.foo
[function: 0x40cfcf90] c.foo
C:foo() = "<static> C.static:foo"
c:foo() = "<instance> C:foo"
c.value = "<static> C.static:foo"
---
[function: 0x40cfcf90] D.static.foo
[function: 0x40cfcf90] D.foo
[function: 0x40cfcf90] d.foo
D:foo() = "<instance> C:foo"
d:foo() = "<instance> C:foo"
d.value = "<instance> C:foo"

You can see D's static functions addresses all point to C's instance functions and initialization of D's instances calls them, resulting in a wrong d.value (it should be the same as c.value: "<static> C.static:foo").

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