lua-users home
lua-l archive

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


On Thu, Jan 19, 2012 at 12:25, Sam Roberts <vieuxtech@gmail.com> wrote:
> On Thu, Jan 19, 2012 at 9:27 AM, Axel Kittenberger <axkibe@gmail.com> wrote:
>> Compare
>> a.b.c.d.e[#] = 1
>> with
>> a.b.c.d.e[a.b.c.d.e.length] = 1
>>
>> Where the former only walks to look up chain through once, to gain
>> same performance with current lua, you need a temporary variable.
>
> Or:
>
> table.insert(a.b.c.d.e, 1)
>
> Sam
>

I like the t[#] --> t[#t] idea, since it eliminates the repetition of
t (can be annoying when t is some long name) and lets you write e.g.
t[#], t[#+2], so it's more flexible than just t[] --> t[#t].

What the original post did remind me of is that I'd love to see t[nil]
and t[] be syntactically valid, passing nil (or maybe in the second
case, no argument) to __index or __newindex, erroring only if those
aren't set (or they throw an error, of course). Maybe even t[1,2,3] to
pass multiple arguments; then you could do crazy things like:
t[] = a or t[nil] = a --append to t
t[x,y] = t[x][y] but with fewer lookups
t[1,2,3] = a,b,c or maybe t[1,2,3] = {a,b,c} instead of t[1], t[2],
t[3] = a, b, c. You could also use t[{1,2,3}], but this is ugly.

-- 
Sent from my toaster.