Page 1 of 1

Alter the class.inherit function

Posted: Sat Dec 24, 2011 5:58 am
by yufra
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:

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})
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:

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 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...

Re: Alter the class.inherit function

Posted: Sat Dec 24, 2011 9:26 am
by darkgod
Yes there is: extreme sluggishness :/
It was like that when I first made the class system and when I switched to cached method it was so much faster.
So basically: do not touch it or the world will come to an end, dogs will make out with cats, cows will eat small children and acidic rains of blood will fall upon all mankind.

As for your problem you can probably fix the code to cache the inherited stuff too, but again *DO NOT FEED THE D..* err .. *DO NOT REMOVE CACHING* :)

Re: Alter the class.inherit function

Posted: Sat Dec 24, 2011 4:52 pm
by yufra
Alright, how about this recursive caching?

Re: Alter the class.inherit function

Posted: Sun Dec 25, 2011 6:03 am
by yufra
I'm testing and tweaking the recursive caching, so don't take it quite yet.

Re: Alter the class.inherit function

Posted: Sun Dec 25, 2011 5:09 pm
by yufra
Here is the newest patch of the recursive inheritance caching. I've tested it in the example module and in ToME and there were no obvious problems.