lua-users home
lua-l archive

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

On Jan 3, 2017, at 8:24 PM, 聂威 <> wrote:

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?

It certainly is a source if confusion until you notice that not only are pack and unpack *not* the inverse of each other... but that they are not available consistently across Lua implementations. Here's what I always use, from :

    local pack = table.pack or function (...)
      return { n = select ("#", ...), ...}

    local table_unpack = table.unpack or unpack
    local function unpack (t, i, j)
      return table_unpack (t, tointeger (i) or 1, tointeger (j) or len (t))

Why not write a PowerPatch for the lua-wiki that fixes the inconsistency in core too?

thanks and happy new year!

Happy New Year!