lua-users home
lua-l archive

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


2010/5/10 steve donovan <steve.j.donovan@gmail.com>:
> On Sun, May 9, 2010 at 11:55 PM, HyperHacker <hyperhacker@gmail.com> wrote:
>> value = tbl[] doesn't necessarily have to be valid, but perhaps it
>> could be a shortcut for value = next(tbl)?
>
> The trouble is, everyone sees different possibilities ;)
>
> I see tbl[] on the RHS as meaning tbl[#tbl] because I've been missing
> Python's tbl[-1]
>
> Still can't see why one can't just say append = table.insert and use
> that; it's almost as fast.
>
> steve d.
>

Of course you can, still, table.getn got replaced by the # operator,
even though you could have typed getn(x) for the same functionality as
#x.






2010/5/10 Juris Kalnins <juris@mt.lv>:
> 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.
>

Hm. Difficult.  To the called metatable function, the input of x[y,z]
= v would look pretty similar to x[y,z,v] = nil. And if you'd pass
more arguments than expected by your index function, you'd get some
pretty "interesting" results ... so more or less, your index function
would be required to accept (...) arguments and then expect the last
value as the value to be assigned.