[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: New metamethod for method lookup
- From: steve donovan <steve.j.donovan@...>
- Date: Sun, 15 Nov 2009 11:59:20 +0200
Naturally, this has been discussed before:
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.