Skip to content

Instantly share code, notes, and snippets.

@kaeza

kaeza/what.lua Secret

Created January 26, 2017 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaeza/57a105a5a6370069afde664758df46ea to your computer and use it in GitHub Desktop.
Save kaeza/57a105a5a6370069afde664758df46ea to your computer and use it in GitHub Desktop.
local classes = { }
local function class(name)
return setmetatable({
extends = function(self, super)
self.__super = classes[super]
return self
end
}, {
__call = function(self, def)
if self.__super then
getmetatable(self).__index = self.__super
end
classes[name] = setmetatable(def, { __index=self })
end,
})
end
local function new(name)
return setmetatable({}, {
__call = function(self, ...)
local C = classes[name]
local inst = setmetatable({}, {__index=C})
if C.new then
C.new(inst)
end
return inst
end,
})
end
class "Foo" {
x = "asdf",
getx = function(self)
return self.x
end,
}
class "Bar" : extends "Foo" {
x = "fdsa",
}
class "Baz" : extends "Foo" {
x = 1234,
}
local foo = new "Foo" ()
local bar = new "Bar" ()
local baz = new "Baz" ()
print(foo:getx(), bar:getx(), baz:getx())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment