MiddleClass: fixed nonworking implicit instantiation (Object() instead of Object:new())

This commit is contained in:
kikito 2010-02-13 17:32:37 +00:00
parent b9248f5cc1
commit df05036c61

View File

@ -7,18 +7,19 @@
local classes = setmetatable({}, {__mode = "k"}) -- weak table storing references to all declared classes
-- The 'Object' class
Object = { name = "Object",
Object = { name = "Object" }
-- creates a new instance
new = function(class, ...)
Object.new = function(class, ...)
assert(classes[class]~=nil, "Use class:new instead of class.new")
local instance = setmetatable({ class = class }, class.__classDict) -- the class dictionary is the instance's metatable
instance:initialize(...)
return instance
end,
end
-- creates a subclass
subclass = function(superclass, name)
Object.subclass = function(superclass, name)
assert(classes[superclass]~=nil, "Use class:subclass instead of class.subclass")
if type(name)~="string" then name = "Unnamed" end
@ -48,7 +49,7 @@ Object = { name = "Object",
rawset(classDict, methodName, method)
end,
__tostring = function() return ("class ".. name) end,
__call = theClass.new
__call = Object.new
})
-- instance methods go after the setmetatable, so we can use "super"
theClass.initialize = function(instance,...) super.initialize(instance) end
@ -56,12 +57,12 @@ Object = { name = "Object",
classes[theClass]=theClass --registers the new class on the list of classes
return theClass
end,
end
-- Mixin extension function - simulates very basically ruby's include(module)
-- module is a lua table of functions. The functions will be copied to the class
-- if present in the module, the included() method will be called
includes = function(class, module)
Object.includes = function(class, module)
assert(classes[class]~=nil, "Use class:includes instead of class.includes")
for methodName,method in pairs(module) do
if methodName ~="included" then class[methodName] = method end
@ -69,7 +70,6 @@ Object = { name = "Object",
if type(module.included)=="function" then module:included(class) end
end
}
classes[Object]=Object -- adds Object to the list of classes