Add optional `inherits' argument to class.

`inherits' is basically a shortcut to Foo = class{...}
Foo:inherit(supers), i.e. those have the same effect:

Foo:class{ inherits = {super1, super2} }
Bar = class{}
Bar:inherit(super1, super2)
This commit is contained in:
Matthias Richter 2011-03-12 23:59:43 +01:00
parent 442fdf3d2f
commit 94da317ed9

View File

@ -25,17 +25,27 @@ THE SOFTWARE.
]]-- ]]--
local setmetatable, getmetatable = setmetatable, getmetatable local setmetatable, getmetatable = setmetatable, getmetatable
local type, assert, pairs = type, assert, pairs local type, assert, pairs, unpack = type, assert, pairs, unpack
local tostring, string_format = tostring, string.format local tostring, string_format = tostring, string.format
module(...) module(...)
local function __NULL__() end local function __NULL__() end
function new(constructor) function new(constructor)
-- check name and constructor
local name = '<unnamed class>' local name = '<unnamed class>'
local super = {}
if type(constructor) == "table" then if type(constructor) == "table" then
if constructor.name then name = constructor.name end local args = constructor
constructor = constructor[1] name = args.name or name
if args.inherits then
-- trick: if arg.inherits has a metatable, it is a class
-- instead of a table (because of the function interface)
if getmetatable(args.inherits) then
super = {args.inherits}
else
super = args.inherits
end
end
constructor = args[1]
end end
assert(not constructor or type(constructor) == "function", assert(not constructor or type(constructor) == "function",
string_format('%s: constructor has to be nil or a function', name)) string_format('%s: constructor has to be nil or a function', name))
@ -58,11 +68,12 @@ function new(constructor)
__tostring = function() return tostring(name) end __tostring = function() return tostring(name) end
} }
inherit(c, unpack(super))
return setmetatable(c, meta) return setmetatable(c, meta)
end end
function inherit(class, interface, ...) function inherit(class, interface, ...)
if not interface then return end if not interface or type(interface) ~= "table" then return end
-- __index and construct are not overwritten as for them class[name] is defined -- __index and construct are not overwritten as for them class[name] is defined
for name, func in pairs(interface) do for name, func in pairs(interface) do