Alter the class.inherit function

Moderator: Moderator

Post Reply
Message
Author
yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Alter the class.inherit function

#1 Post 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...
Attachments
class_multiple_inheritance_handler.txt
(969 Bytes) Downloaded 246 times
<DarkGod> lets say it's intended

darkgod
Master of Eyal
Posts: 10751
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: Alter the class.inherit function

#2 Post 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* :)
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: Alter the class.inherit function

#3 Post by yufra »

Alright, how about this recursive caching?
Attachments
class_multiple_inheritance_recursive_caching.txt
(1.86 KiB) Downloaded 269 times
<DarkGod> lets say it's intended

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: Alter the class.inherit function

#4 Post by yufra »

I'm testing and tweaking the recursive caching, so don't take it quite yet.
<DarkGod> lets say it's intended

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: Alter the class.inherit function

#5 Post 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.
Attachments
class_recursive_inheritance.txt
(2.68 KiB) Downloaded 268 times
<DarkGod> lets say it's intended

Post Reply