Packing unpacked values

98 views
Skip to first unread message

Alexander Mashin

unread,
Jun 17, 2024, 9:54:45 AMJun 17
to lua-l
It seems that re-packing unpacked values back into a table does not work correctly in Lua 5.1, 5.2, 5.3, 5.4 and LuaJit.

The last unpacked value is lost when a new table is constructed and the value is followed by another value:

return table.concat { table.unpack { 1, 2 }, 3 }

returns '13' rather than expected '123'.

return table.concat { table.unpack { 1, 2 } }

correctly returns '12'.

Alexander Gladysh

unread,
Jun 17, 2024, 10:01:07 AMJun 17
to lu...@googlegroups.com
On Mon, Jun 17, 2024 at 4:54 PM Alexander Mashin <alex....@gmail.com> wrote:
It seems that re-packing unpacked values back into a table does not work correctly in Lua 5.1, 5.2, 5.3, 5.4 and LuaJit.

The last unpacked value is lost when a new table is constructed and the value is followed by another value:

return table.concat { table.unpack { 1, 2 }, 3 }

This is as expected. Try it with arguments in different order:

return table.concat { 3, table.unpack { 1, 2 } }

lua.org seems down for me right now, so here's a WebArchive link: https://web.archive.org/web/20240612223629/https://lua.org/manual/5.4/manual.html#3.4.12

Best,
Alexander.

Scott Morgan

unread,
Jun 17, 2024, 10:07:31 AMJun 17
to lu...@googlegroups.com
That's expected behaviour.

Try:
return table.concat { 1, table.unpack { 2, 3 } }

See section 3.4.11 of the 5.4 manual
> If a vararg expression is used inside another expression or in the
> middle of a list of expressions, then its return list is adjusted to
> one element.

Scott

Sainan

unread,
Jun 17, 2024, 10:34:55 AMJun 17
to lu...@googlegroups.com
May I suggest writing more sane code?

local t = { 1, 2 }
table.insert(t, 3)
print(table.concat(t)) --> 123

Rett Berg

unread,
Jun 17, 2024, 5:50:02 PMJun 17
to lua-l
That isn't more sane code, it's more Sainin code.

It's totally understandable that the OP would be confused.

Rett Berg

unread,
Jun 17, 2024, 5:50:41 PMJun 17
to lua-l
Agh, I spelled your name wrong sorry. More "Sainan" code (was a pun)

Sainan

unread,
Jun 17, 2024, 6:10:28 PMJun 17
to lu...@googlegroups.com
Not to be too egoistical, but I think trying to write more "Sainan" code is also a good thing to strive for. 😉
Reply all
Reply to author
Forward
0 new messages