lua-users home
lua-l archive

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


Vaughan McAlley <ockegheim@gmail.com> wrote:
(26/06/2009 07:08)

>What might be nice is an optional argument to unpack that replaces #
>if it is present. Also table.concat and perhaps other functions that
>rely on # rather than stopping at the first nil. So this sort of thing
>would be possible:
>
>t = { "A", "B", "C", "D", "E", "F", "G", "H" } -- pretend this table
>is being recyled
>-- this time we only want to concatenate three elements:
>
>t[1] = "U"
>t[2] = "V"
>t[3] = "W"
>t[4] = nil -- may not be necessary with the second argument to table.concat
>
>print(table.concat(t, 3)) -- use 3 instead of #t
>
>> UVW
>
>At the moment table.concat(t) uses #t (which is still 8) and gives an
>error when it meets the nil at t[4]. As far as I can tell, this change
>would be easy to implement, and would soften the occasional
>unpredictability of the # operator. It could also be used with unpack,
>padding with nils as necessary.

That looks similar to what I was exploring a couple of days ago. I don't know whether my hope is a reasonable one, but I'd like to see it set # to the first nil minus 1, and a way to say #t=n so we can do what used to be done by setn, because over-riding # is useful. Alternatively, your extra argument scheme looks ok to me, but would be nice to have in ipairs() too. I think the #t=n might be preferable though, as a lot of Lua users already know setn (and probably also wish it still existed).