Last active
August 29, 2015 14:18
-
-
Save adrian-alberto/77b818d68b709cfb1bd0 to your computer and use it in GitHub Desktop.
My custom OOP framework for Lua.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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