[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Inheritance of metamethods in idiomatic Lua OOP--not as slick as one would expect
- From: Griffin Rock <abcq2@...>
- Date: Tue, 31 Mar 2020 01:21:01 +0000
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