lua-users home
lua-l archive

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


I am working with a function that returns a vararg and using those returns to construct a table. While I was working on this I found a behavior I was not expecting. I have created a simple example to demonstrate the behaviour (see below). I expected the table to constructed with all the varargs plus the "flag", but it only takes the first vararg and the "flag" key/value pair. I bet Lua has a special helper syntax to allow simple table constructions using only varargs, but I cant seem to find the documentation on this subject. Can anyone confirm this behaviour is sane and documented? Thoughts?

<code>
function foo()
  return 1, 2, 3
end

broken =
{
  foo(),
  flag = true,
}

working_vararg_at_end =
{
  flag = true,
  foo(),
}

working =
{
  foo()
}

print( "#broken", #broken, table.maxn( broken ) )
for _, element in ipairs( broken ) do
  print( element )
end

print( "#working_vararg_at_end", #working_vararg_at_end )
for _, element in ipairs( working_vararg_at_end ) do
  print( element )
end

print( "#working", #working )
for _, element in ipairs( working ) do
  print( element )
end

</code>
--
Regards,
Ryan