lua-users home
lua-l archive

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


On Sun, 09 May 2010 18:36:02 +0300, Klaus Ripke <paul-lua@malete.org> wrote:


Could x[] be syntactic sugar for x[#x+1] ?

Could indexing anything but a table, or at least indexing nil,
return nil instead of throwing an error?
I don't see why x.z conveniently gives nil, if there is no z in x,
but x.y.z requires an extra check for x.y .

Actually, it would be more powerful to have [] operator take arbitrary number of
values, just like () does.
Then:
x.y.z --> x[y,z] --> x_metatable.__index(x,y,z)
x.y.z = v --> x[y,z] = v --> x_metatable.__newindex(x,y,z,v)
x[] --> x_metatable.__index(x)
etc.

And have default behavior forward indexing calls to nested
elements, just like it works now.

This would allow a simple way to implement things in the wishlist,
plus simplify many cases that currently require use of proxy objects.