Skip to content

Instantly share code, notes, and snippets.

@katlogic
Created May 10, 2014 18:32
Show Gist options
  • Save katlogic/6552ad8cc8087940b736 to your computer and use it in GitHub Desktop.
Save katlogic/6552ad8cc8087940b736 to your computer and use it in GitHub Desktop.
-- class implementation
local function class(t)
t.__index = t
return t
end
local function extend(dst,src)
for k,v in pairs(src) do
if type(k) == "string" and k:sub(1,1) ~= "_" then
dst[k] = dst[k] or v
end
end
dst.__index = dst
return dst
end
-- example class Foo
local Foo = class{
attr1 = 1,
attr2 = 2,
}
function Foo:getattr1()
return self.attr1
end
function Foo:getattr2()
return self.attr1
end
-- example class Bar which inherits from Foo
local Bar = extend({
attr3 = 3,
attr1 = 4,
}, Foo)
function Bar:getattr3()
return self.attr3
end
-- usage
inst = setmetatable({attr3=1337}, Bar)
print(inst:getattr3(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment