2010/5/9 Florian Weimer <fw@deneb.enyo.de>:
* Klaus Ripke:
Could x[] be syntactic sugar for x[#x+1] ?
It would make appending to lists so much nicer.
"x[] = 5" looks about as cryptic to newcomers as "x[#x+1] = 5". I'm
not sure if it is an improvement. If this operation is so common that
it warrants its own syntax, something completely different might be
better. But then, "table.append(x, 5)" doesn't look too bad, either.
I don't care about beginners, but
some_complex_name[#some_complex_name+1] = "foo"
is surely worse than
some_complex_name[] = "foo"
not to mention
some_function_returning_table(...)[] = "OK"
... which requires currently temporary variables.
The [] operator is maybe the one and only thing that I actually like
about PHP. It makes sense in my opinion, as it's a very common
operation when operating with stacks or queues.
It could be somewhat done right now using metatables and using a
special key, e.g.
arr[nil] = "append"
but, a [] operator looks more useful to me than fidling around with
metatables for such a basic functionality.
Adding another thing to the wishlist: table.remove should allow a
third parameter to allow efficient table slicing, e.g.
table.remove(arr,1,100)
instead of
for i=1,100 do table.remove(arr,1) end
(assuming that arr is really huge)
Cheers,
Eike