|
Am 20.10.2014 um 11:42 schröbte Paco Zamora Martínez:
Dear Lua community,
Hi!
In order to make it clearer, I have written two minimal examples where this behavior is present: *setmetatable(getmetatable(metainstance.__index).__index, {* * __index = {* * baz = function(self) return self.v end,* * },* *})* *setmetatable(getmetatable(metainstance.__index).__index, {* * __index = function(self,key)* * if key == "baz" then return rawget(self,v) end* * end,* *})*
Those two are not equivalent! The second one should be something like setmetatable(getmetatable(metainstance.__index).__index, { __index = function(self,key) if key == "baz" then return function(self) return rawget(self,"v") end end end, }) and then you get your expected output of: > 5 > 5 > 5A method call using `:` consists of two operations: The fetching of the function and the function call. The `self` argument of the function call will always be the top level object, but the arguments of the `__index` call might not be the top level object depending on how deep the chain is. So when you try to combine fetching and calling, you can end up with the wrong `self`.
Philipp