[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __index returns truncated to one, why?
- From: Hisham <h@...>
- Date: Thu, 10 Apr 2014 23:59:21 -0300
On 10 April 2014 04:06, duz <duz@sol-3.de> 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.
An alternative would be to do things the other way around: use __call
as the accessor and a tracker:add() function to add data. Might be a
good idea if you read data more often than you write it.
tracker = my_fancy_tracker(3) -- three tracks...
tracker:add(1, 2, 3)
tracker:add(7, 8, 9)
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
-- Hisham