lua-users home
lua-l archive

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


First time poster here.. be gentle.

For a particular oo system I was experimenting with, I found the current __index
metamethod to not work quite as desired. The difference is best explained through
an example:

 local proxy = setmetatable({}, {__index = function(tab) return tab.greeting end})
 local test = setmetatable({ greeting = "hello" }, {__index = proxy})

The current implementation would print nil, I propose it would be more meaningful
for the end __index metamethod to be called with the table that instigated the
__index loop, rather than table that holds the function. The performance
difference is negligible, and the change can be implemented with one line of code
and one changed variable name.

The actual system is one where:

setmetatable( class, {__index = function ... end} )
setmetatable( object, { __index = class } )

which allows high speed lookups to class values (eg functions), yet will still
call getter functions for unrecognised fields. By passing the function class, as
is the current situation, the information of which object instigated the lookup
is lost.  In the case that there is an advantage in the current method (I haven't
been able to think of a situation yet however) - the class would always be able
to be found through getmetatable. Any thoughts?