|
We (Lua users at Thermo-Fisher) were surprised recently to find that the __index metamethod of the metatable’s metatable isn’t used when looking for things like __newindex metamethods. base={} setmetatable(base,{__newindex=someusefulthing})
function base:New() self.__index=self return setmetatable({},self) end singlyderived=based:New() instanceofsinglyderived=singlyderived:New() we’d expect a newindex operation for instanceofsinglyderived to look in its metatable (singlyderived) for a __newindex key-value pair. On not finding it, the __index metamethod would be followed, pointing to base, which has a __newindex
that we copied in. The only place where we have to do any shenanigans is in the base class.
Maybe the performance hit for always respecting the metatable’s metatable is too great? |