lua-users home
lua-l archive

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



On 28-Nov-06, at 10:48 PM, al_lea wrote:

hi,

I'd just port some code from lua5.0 to 5.1 and found that unpack(arg) doesn't
work as expected.

code sample:
g_load_new =
{
  [1] = "arcaves.lua",
  [4] = 1,
  n = 4,
 }

function foo(...)
 print(unpack(arg))
 print(arg.n)
end

foo(unpack(g_load_new))
-- end --
in 5.0 the output is:
arcaves.lua nil nil 1
4

in 5.1 the output is:
arcaves.lua
1

Is there some way to make the unpack works well with nil holes?

Try:
  unpack(g_load_new, 1, g_load_new.n)

I don't quite understand why you're packing an array in order to unpack it in order to send it to a varargs function. If you want to find out how many arguments you received, use:
  select('#', ...)

If you want to convert ... to an array with explicit length, use:
  {n = select('#', ...), ...}

(which is essentially 5.0 compatibility)