metatable

Lua metatables and metamethod - How to call a different member function

天涯浪子 提交于 2019-12-02 02:33:15
I have the following Class local PROGRESS = {} PROGRESS.__index = function(self,key) if key~="__group" and self.__group[key] then return self.__group[key] else return rawget(self,key) end end What this does is when You access table[key] it performs a lookup in table.__group (which is an object of another class) and returns table.__group[key] ,if it is not nil. Now I am trying to do the same for member functions. i.e If I call table:key() a lookup must be performed in table.__group and if the function is present, then table.__group:key() should be called. How do I accomplish this? I tried to do

Confusion of using “.” notation with __index and namespace in Lua

谁说我不能喝 提交于 2019-11-29 15:50:08
I am confused of the following two syntaxes using "." From what I understand, __index is called when a key doesn't exist in a table but exists in its metatable. So why does the list table call __index and then assign itself to list.__index ? list = {} list.__index = list setmetatable(list, { __call = function(_, ...) local t = setmetatable({length = 0}, list) for _, v in ipairs{...} do t:push(v) end return t end }) function list:push(t) if self.last then self.last._next = t t._prev = self.last self.last = t else self.first = t self.last = t end self.length = self.length + 1 end . . . local l =

Confusion of using “.” notation with __index and namespace in Lua

落爺英雄遲暮 提交于 2019-11-28 09:24:04
问题 I am confused of the following two syntaxes using "." From what I understand, __index is called when a key doesn't exist in a table but exists in its metatable. So why does the list table call __index and then assign itself to list.__index ? list = {} list.__index = list setmetatable(list, { __call = function(_, ...) local t = setmetatable({length = 0}, list) for _, v in ipairs{...} do t:push(v) end return t end }) function list:push(t) if self.last then self.last._next = t t._prev = self