lua-users home
lua-l archive

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


Hi all,

Naturally, this has been discussed before:

http://lua-users.org/lists/lua-l/2009-06/msg00486.html

Basically, there is a need to separate method lookup from field
lookup, but in such a way that the old method is always available.

Here is the problem case: say you wish to wrap a set as an object. Say
also that you want s[val] to _reliably and always_ indicate the
presence of val in the set s.  Currently, this cannot be be done.  If
our set has an intersection() method, then s['intersection'] will
always be a false positive, because we look up 'intersection' in the
set's metatable if lookup fails.  So we have to write this operation
as a method call, say s:get(val) or s:exists(val) that uses rawget
internally, which isn't so pretty or intuitive.

The proposal which seems most Lua-like is to add a __methindex which
when enabled will catch all method lookups. In its absence, then
__index works for all lookups.

I confess that I don't know what hoop-jumping the current Lua
implementation would have to do to implement __methindex.( It may be
easier to implement David Manura's suggestion of a __mcall)   Also,
this is probably a late feature request, since I suspect that Lua 5.2
is already nearly ready for release.

steve d.