lua-users home
lua-l archive

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


On Wed, 2010-12-08 at 18:23 +0900, Miles Bader wrote:
> Arseny Vakhrushev <arseny.vakhrushev@gmail.com> writes:
> >>> Actually, it works well. I mean {...}/unpack() is fully reversible.
> >> 
> >> You're just lucky with your data
> >> 
> >> $ lua -e 'print(select("#", unpack{ 1, nil, 3 }))'
> >> 3
> >> 
> >> $ luajit2 -e 'print(select("#", unpack{ 1, nil, 3 }))'
> >> 1
> >> 
> >
> > Well, I was talking about vanilla Lua. Anyway, the above difference is
> > very weird because it doesn't allow to postpone the usage of varargs,
> > for instance, in some coroutine create/resume loop where arguments could
> > be anything generic. Fortunately, I rely on this behavior only within a
> > testing framework which is run under plain Lua.
> 
> Sadly, the same is true of vanilla lua:
> 
>    $ lua -v
>    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> 
>    $ lua -e 'print(select ("#", unpack{ 1, nil, nil, nil, nil, nil,nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 3 }))'
>    1

You can always use the additional parameters of unpack to specify how
many values you want to unpack:

$ lua -e 'print(select ("#", unpack({ 1, nil, nil, nil, nil, nil,nil,
nil, nil, nil, nil, nil, nil, nil, nil, nil, 3 }, 1, 17)))'
17

What I tend to do is following:

function f(...)
  local args = { n=select('#', ...), ... }
  -- do something with args
  return unpack(args, 1, args.n)
end