lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


bennett.kalafut> we’d expect a newindex operation ... to look in its metatable ...
bennett.kalafut> The only place where we have to do any shenanigans is in the base class.

local c = {} -- use a common metatable for every class
function c:__index (i)
  if i ~= 'parent' then return self.parent[i]; end
end
local function o (meta)
  c[meta] = function (self, ...)
    -- redirect metamethod lookup to an index operation
    return self[meta] (self, ...)
  end
  return o
end
o "__add" "__sub" "__mul" "__div" "__unm"
  "__band" "__bor" "__shl" "__shr" "__bnot"
  "__eq" "__lt" "__le"
  "__call"
function derive (t)
  return setmetatable({parent = t}, c)
end