Advertisement

An easy question for Lua experts

Started by November 21, 2011 03:48 AM
2 comments, last by ddn3 13 years, 3 months ago
The below code snippet works, but I was wondering if its the most efficient way of doing things, both in the 'new' functions and in the 'inherit' function.


ClassA = { a = 1, b = 2 }
ClassA_mt = { z = 9 }

function ClassA:new()
ClassA_mt.__index = ClassA_mt
local new = setmetatable(ClassA, ClassA_mt)
return new
end

ClassB = { c = 3, d = 4 }
ClassB_mt = { u = 7 }

function ClassB:new()
ClassB_mt.__index = ClassB_mt
local new = setmetatable(ClassB, ClassB_mt)
return new
end

function inherit(t1, t2)
local mt1 = getmetatable(t1)
local mt2 = getmetatable(t2)
for k,v in pairs(mt2) do mt1[k] = v end
for k,v in pairs(t2) do t1[k] = v end
mt1.__index = mt1
setmetatable(t1, mt1)
end

cla = ClassA:new()
clb = ClassB:new()

inherit(cla, clb)

print(cla['d'])
print(cla['u'])
The only way to know which object implementation is better is to benchmark it. I'll give it a go against my object impl and see how it turns out. This one i see copies the metatable for every derived object, some impl just assign it the parents metatable.

This is the implementation I use..



- OO function used for building derived classes
function Misc:inheritsFrom( baseClass )

local new_class = {}
local class_mt = { __index = new_class }

function new_class:create()
local newinst = {}
setmetatable( newinst, class_mt )
return newinst
end

if nil ~= baseClass then
setmetatable( new_class, { __index = baseClass } )
end

-- Implementation of additional OO properties starts here --

-- Return the class object of the instance
function new_class:class()
return new_class;
end

-- Return the super class object of the instance, optional base class of the given class (must be part of hierarchy)
function new_class:baseClass(class)
return new_class:_B(class);
end

-- Return the super class object of the instance, optional base class of the given class (must be part of hierarchy)
function new_class:_B(class)
if (class==nil) or (new_class==class) then
return baseClass;
elseif(baseClass~=nil) then
return baseClass:_B(class);
end
return nil;
end

-- Return true if the caller is an instance of theClass
function new_class:_ISA( theClass )
local b_isa = false

local cur_class = new_class

while ( nil ~= cur_class ) and ( false == b_isa ) do
if cur_class == theClass then
b_isa = true
else
cur_class = cur_class:baseClass()
end
end

return b_isa
end

return new_class
end




-ddn
Advertisement
Thanks for your reply. I've seen that version before. Not counting the added functions such as _ISA it structurally does the same thing. Benchmarks are comparable if I rip the extra functionality out of your version. Looks like i am doing things fine. :)
Yep they are comprable, though I like how yours gets around the base class dispatch problem, it copies the base class functions to the childs metatable, in mine it walks the inheritance tree to dispatch a base class function. I might just give that method a try.

Laters!


-ddn

This topic is closed to new replies.

Advertisement