Skip to content

Instantly share code, notes, and snippets.

@kengonakajima
Created February 9, 2012 07:15
Show Gist options
  • Save kengonakajima/1778058 to your computer and use it in GitHub Desktop.
Save kengonakajima/1778058 to your computer and use it in GitHub Desktop.
oop system from luvit
-- OOP system from luvit
local Object = {}
Object.meta = {__index = Object}
function Object:create()
local meta = rawget(self, "meta")
if not meta then error("Cannot inherit from instance object") end
return setmetatable({}, meta)
end
function Object:new(...)
local obj = self:create()
if type(obj.initialize) == "function" then
obj:initialize(...)
end
return obj
end
function Object:extend()
local obj = self:create()
obj.meta = {__index = obj}
return obj
end
-- example/benchmark follows.
local Rectangle = Object:extend()
local Square = Rectangle:extend()
local Square2 = Square:extend()
local Square3 = Square2:extend()
local Square4 = Square3:extend()
local Square5 = Square4:extend()
local Square6 = Square5:extend()
local st = os.clock()
for i=1,100000 do
local s = Square6:new(1)
end
local et = os.clock()
print("t:", (et-st))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment