Comments (3)
Replace:
else rawget(self, key)
with:
else return rawget(C, key) or C.super[key]
from classic.
@rxi Thank you so much sir!
from classic.
@rxi I'm sorry, but this solution doesn't work well:
local MiddleClass = require "middleclass"
local M1 = MiddleClass("M1")
function M1:initialize()
self.pos = {2, 4}
self.size = {6, 8}
end
function M1:__index(key)
if key == "x" then return self.pos[1]
elseif key == "y" then return self.pos[2]
elseif key == "w" then return self.size[1]
elseif key == "h" then return self.size[2]
else return rawget(self, key)
end
end
function M1:__newindex(key, value)
if key == "x" then self.pos[1] = value
elseif key == "y" then self.pos[2] = value
elseif key == "w" then self.size[1] = value
elseif key == "h" then self.size[2] = value
else rawset(self, key, value)
end
end
local M2 = M1:subclass("M2")
local M3 = M2:subclass("M3")
local M4 = M3:subclass("M4")
local m = M4()
assert(m.x == 2)
m.x = 222
assert(m.pos[1] == 222)
print("No errors in MiddleClass.")
local Classic = require "classic"
local C1 = Classic:extend()
function C1:new()
self.pos = {2, 4}
self.size = {6, 8}
end
function C1:__index(key)
if key == "x" then return self.pos[1]
elseif key == "y" then return self.pos[2]
elseif key == "w" then return self.size[1]
elseif key == "h" then return self.size[2]
else return rawget(C1, key) or C1.super[key]
end
end
function C1:__newindex(key, value)
if key == "x" then self.pos[1] = value
elseif key == "y" then self.pos[2] = value
elseif key == "w" then self.size[1] = value
elseif key == "h" then self.size[2] = value
else rawset(self, key, value)
end
end
local C2 = C1:extend()
local C3 = C2:extend()
local C4 = C3:extend()
local c = C4()
assert(c.x == 2)
c.x = 222
assert(c.pos[1] == 222)
print("No errors in Classic.")
No errors in MiddleClass.
luajit: metatables-oop.lua:59: attempt to index field 'pos' (a nil value)
from classic.
Related Issues (15)
- Allow mixins to be separated. HOT 1
- Recursive stack overflow when calling a super method that also calls a super method.
- Upload to luarocks
- Difference between middleclass, classic and oo HOT 3
- using :new() for creating instances silently fails HOT 2
- does this allow for reflection? If so, how?
- Properties HOT 15
- Hide meta methods
- Extending the same base class in two different objects creates weird data duplication problems HOT 2
- Error free as method HOT 4
- is method has problems when value is nil. workaround?
- Multiple inheritance error HOT 1
- Local Functions do not work?
- How to extend a method? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from classic.