Skip to content

Instantly share code, notes, and snippets.

@hashmal
Created March 19, 2011 18:38
Show Gist options
  • Save hashmal/877697 to your computer and use it in GitHub Desktop.
Save hashmal/877697 to your computer and use it in GitHub Desktop.
Quick and dirty mixins in Lua
Mixin = {}
Mixin.__index = Mixin
Mixin.__mixins = {Mixin}
function Mixin:__index (method)
for i=#getmetatable(self).__mixins, 1, -1 do
local m = getmetatable(self).__mixins[i][method]
if m then return m end
end
end
function Mixin:new ()
return setmetatable({}, Mixin)
end
function Mixin:addmeta (mt)
table.insert(self.__mixins, mt)
end
function Mixin:removemeta (mt)
for k, v in pairs(self.__mixins) do
if v == mt then self.__mixins[k] = nil end
end
end
-- Usage
obj = Mixin:new()
obj:addmeta(<table>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment