lua-users home
lua-l archive

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


On Sat, Nov 26, 2011 at 05:16, Patrick Rapin <toupie300@gmail.com> wrote:
>>> 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
>

n does seem a bit redundant when you have __len...

-- 
Sent from my toaster.