Skip to content

Instantly share code, notes, and snippets.

@adrian-alberto
Last active August 29, 2015 14:18
Show Gist options
  • Save adrian-alberto/77b818d68b709cfb1bd0 to your computer and use it in GitHub Desktop.
Save adrian-alberto/77b818d68b709cfb1bd0 to your computer and use it in GitHub Desktop.
My custom OOP framework for Lua.
function class(inheritsFrom)
local c = {}
local c_mt = {__index = c, __tostring = function(obj) if obj.tostring then return obj:tostring() end end}
function c.new(...)
local obj = setmetatable({}, c_mt)
if obj.init then obj:init(...) end
return obj
end
function c.super()
return inheritsFrom
end
function c.instanceOf(class)
return c == class or inheritsFrom and inheritsFrom.instanceOf(class)
end
if inheritsFrom then
if not inheritsFrom.instanceOf then error("Bad superclass.") end
setmetatable(c, {__index = inheritsFrom})
end
return c
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment