lua-users home
lua-l archive

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


Dear All,

I wondered, why all but one values returned from the __index metamethod
are dropped by Lua. Such that a statement like this:

  a, b, c = t[42]

always sets b,c=nil,nil even if t has an __index metamethod which
returns three values if invoked like this.

I'd like to rephrase this as a bug report:

Checking the current Lua reference manual, section

    2.4 – Metatables and Metamethods

Nothing in the description of the __index method mentions such
truncation. The (pseudo?-)Lua code that specifies the semantics
of "index" just returns

   return (h(table, key))

in the relevant situation which would be

   __index(t,42)

in above example and no truncation of the number of results is
indicated. Otherwise Lua quite rightfully takes pride in the
nice feature of being able to return multiple values and every
user expects this.

So not doing it here is inconsistent and probably just a bug.

Cheers,
Dirk


On 04/10/2014 09:06 AM, duz wrote:
Hello All,

I see you're happily making suggestions for changes to Lua,
so I'll drop in mine. Its quite small.

The other day, I built a fancy tracker of some incoming data,
think of a ring buffer. In various flavours. In C and exported
to Lua per userdata.

These beasts were able to record multiple tracks of incoming
data, like a tape machine. Then I thought: How nice, I can
access the most recent set of numbers using [-1] and the first
ever recorded as [1]. In effect, I could traverse the data
back and forth by using indices >0 or <0.

This didn't work for multiple "tracks" of data, as the
multiplie values I was pushing to the Lua Stack from within
__index were truncated to one. For no reason I could see.

tracker = my_fancy_tracker(3)  -- three tracks...
...
tracker (1, 2, 3) -- using __call to eat data.
tracker (7, 8, 9)

Now I was hoping to use __index and allow for this semantics:

   local a, b, c = tracker[-1]

setting a,b,c=7,8,9 and

   local e, f, g = tracker[1]

setting e,f,g=1,2,3

But no luck. The language processing past [] dropped all but
the first value. I had to use an accessor function to be called
like this:

   local a,b,c = tracker:at(1)

This looks comparatively clumsy and is less efficient.

So my request would be:

Please do not truncate the results returned from __index to 1!

Cheers,
Dirk