lua-users home
lua-l archive

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


I just want to say that it is a bad idea to rely on .n as metadata in this case. table.sort is supposed to work on the "array part" only, which means the user should be able to use non-numeric indices for whatever they like.

On Thu, Jan 5, 2017 at 6:00 AM, Xavier Wang <weasley.wx@gmail.com> wrote:
2017-01-04 12:24 GMT+08:00 聂威 <davidxifeng@icloud.com>:
> dear list,
>
> I have noticed that `table.unpack(list [,i [, j]])` will NOT regards the `n`
> field in the `list` table,
>
> I know I can write it like this:
>
> ```
> table.unpack(list, 1, list.n)
> ```
>
> but will it be a good idea to make it read `n` field in table.unpack?
>
> thanks and happy new year!
>
> ps:
>
> table.pack in Lua 5.3 manual:
>
> ```
>
> table.pack (···)
>
> Returns a new table with all parameters stored into keys 1, 2, etc. and with
> a field "n" with the total number of parameters. Note that the resulting
> table may not be a sequence.
>
> ```

So I think both change table.pack and table.unpack will work:

table.pack(...) -> t, n
table.unpack(t [, m], n) -> ...

so table.pack return two value, a table and a length, and table.unpack
could accept two or three arguments.
for unpack if only n given, the 1..n index in table will unpack, and
if m and n given, the m..n index in table will unpack.

--
regards,
Xavier Wang.