Alter the class.inherit function
Posted: Sat Dec 24, 2011 5:58 am
I was busy creating a mod.class.interface.ActorTalents and was getting some very weird behavior. I think I finally have it isolated, and the issue is how the class.inherit function handles multiple inheritances of already inheriting classes. The inheritance scheme is to treat the first argument as the base class, and the following ones as pseudo-base classes. This is the relevant section of code:
The loop over "pairs(_if)" will only look at the entries of that class, but if it is an inherited class all of the entries in the base class will be missed. I propose we not cache and actually use a function handler to deal with multiple inheritance like so:
I haven't tested this, but wanted to get it written down before I lost my train of thought. I'll test tomorrow I guess.
I guess there could have been a reason to prefer caching over this method...
Code: Select all
for i, _if in ipairs(ifs) do
for k, e in pairs(_if) do
if k ~= "init" and k ~= "_NAME" and k ~= "_M" and k ~= "_PACKAGE" and k ~= "new" then
c[k] = e
-- print(("caching interface value %s (%s) from %s to %s"):format(k, tostring(e), _if._NAME, c._NAME))
end
end
end
setmetatable(c, {__index=base})
Code: Select all
local handler = function(self, k)
local result
result = base[k]
if result ~= nil then
return result
end
for i, _if in ipairs(ifs) do
result = _if[k]
if result ~= nil then
return result
end
end
end
setmetatable(c, {__index=handler})
I guess there could have been a reason to prefer caching over this method...