Skip to content

Instantly share code, notes, and snippets.

@FireyFly
Created August 15, 2012 22:16
Show Gist options
  • Save FireyFly/3364183 to your computer and use it in GitHub Desktop.
Save FireyFly/3364183 to your computer and use it in GitHub Desktop.
Lua super-light fancy delegative prototypal inheritance
local Base = require('library').Base
local Person = Base {
-- missing properties: name
speak = function (self)
return "Hello, I am " .. self.name
end
}
local Programmer = Person {
-- missing properties: name
speak = function (self)
-- "super call"
return Person.speak(self) .. " and I like programming"
end
}
local me = Programmer { name = "FireFly" }
print(me:speak() .. "!") -- Hello, I am FireFly and I like programming!
local function inherit(self, tbl)
return setmetatable(tbl, { __index = self, __call = inherit })
end
local Base = inherit({}, {}) -- our base type
return { inherit = inherit, Base = Base }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment