lua-users home
lua-l archive

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


On 04/10/2014 03:06 PM, 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



my opinions:

the `metamethod' mechanism in Lua is FALLBACK, not OVERRIDE.
the __index metamethod is fallback for non-existance keys.
it has the same semantic of the index operator(i.e. the brackets).

metamethods are not C++'s overloaded.operators.(don't take me wrong:
C++ is good, I like C++ and use it often. I also like Lua, and use
it often, too. But I REALLY don't want Lua to become C++.).

if you want a function call semantic, then use a function call.
if you use the index operator, you get the indexing semantic.
even though the indexing action caould be performed in the form of
a metamethod, it is still a indexing action, not a function call.