Created
August 15, 2012 22:16
-
-
Save FireyFly/3364183 to your computer and use it in GitHub Desktop.
Lua super-light fancy delegative prototypal inheritance
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
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! |
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
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