lua-users home
lua-l archive

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

On Wed, Jun 15, 2011 at 1:57 PM, Fredrik Widlund
<> wrote:
> Hi,
> Could someone explain if the following is by design:
> Lua 5.1.4  Copyright (C) 1994-2008, PUC-Rio
>> a={1,2,3}
>> b={4,5,6}
>> c={unpack(a), unpack(b)}
>> print(table.concat(c, ","))
> 1,4,5,6
> It seems the second unpack starts "writing" its members to position i+1 and thus overwrites the members of the first unpack? Is this really correct behaviour?
> Of course:
>> c={unpack(a)}
>> print(table.concat(c, ","))
> 1,2,3

The following bit of the reference manual is relevant:

"Both function calls and vararg expressions can result in multiple
values. If an expression is used as a statement (only possible for
function calls (see §2.4.6)), then its return list is adjusted to zero
elements, thus discarding all returned values. If an expression is
used as the last (or the only) element of a list of expressions, then
no adjustment is made (unless the call is enclosed in parentheses). In
all other contexts, Lua adjusts the result list to one element,
discarding all values except the first one."

Both unpack(a) and unpack(b) return possible multiple results. In the
table constructor, the first unpack is adjusted to one element, but
all of the returns of unpack(b) are used. That's why you are seeing
this behaviour.

- Jim