lua-users home
lua-l archive

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

On 10-Jul-07, at 7:58 PM, David Manura wrote:

Jerome Vuarand <jerome.vuarand <at>> writes:
Another solution that no one else proposed is to pass several keys to
your __index metamethods. You can do that by creating a table

The following variant of that approach avoids constructing a table and is
reasonably straightforward and efficient:

  value, delta = t[i], t[-i]

(assuming the key i is a positive integer so that the domains of i
and -i are mutually exclusive).

I also like the common solution:

  value, delta = t:pair(i)    -- just get used to it ;)

Yet another possibility is returning a tuple, implemented
as a function.

function pair(a, b) return a, b end

t = {pair(3, 0.25), pair(4, 1.0), pair(-3, 0.8)}

for _, p in ipairs(t) do
  local value, delta = p()