This might be usefull for a OO syntax, as syntax with : is a bit confusing.
--This is derived from Salmaso Raffaele by Timm Felden
--the origin and the license can be found at http://thread.gmane.org/gmane.comp.lang.lua.general/12344/focus=12344
function Class(super, typename)
-- create a new class description
local class = {}
-- set the superclass (for object/function inheritance)
setmetatable(class, {
__index = super,
__call = function(this, ...) --a call of Class is similar to C++'s new class(...)
local tmp = {}
setmetatable(tmp, class)
if this.new then
local err = this.new(tmp, ...); -- non-nil return values are exceptions and will be printed
if err then
print(err);
return nil;
end
end
return tmp
end
})
class.__type = typename
if super and super.__tostring then --einige Metamethods müssen scheinbar von Hand geerbt werden
class.__tostring = super.__tostring
end
class.__index = function(table, key)
local r = class[key]
if type(r) == 'function' then
local f = function(...) return r(table, ...) end
table[key] = f
return f
else
table[key] = r
return r
end
end
return class
end
usage:
A = class(nil, "A");
A.new = function(this, name) this.name = name end
A.__add = function(this, that) return A(this.name .. that.name) end
A.print = function(this) print(this.name) end
B = class(A, "B")
B.print = function(this) print(this.name .. " is a B!") end
hope it works for you and is usefull:)