lua-users home
lua-l archive

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


>> print(table.unpack(table.pack(1,2,3)))
> 3

After looking a little bit more, this is not a bug but a pitfall.
Function table.pack returns two values: a table and its length.
When passing it directly to table.unpack, the second result is taken
as the starting index.
An extra parenthesis solves the problem.

> print(table.unpack((table.pack(1,2,3))))
1       2       3

Still, is there a way to avoid that pitfall ?
I will probably not be the last to try that apparently logical syntax...

My proposal [1] was that table.pack should set __len metamethod
instead of returning the value or setting "n" field.
Wouldn't it be less risky ?

[1] http://lua-users.org/lists/lua-l/2011-06/msg01386.html