[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: pack/unpack
- From: Jim Whitehead II <jnwhiteh@...>
- Date: Wed, 15 Jun 2011 14:02:02 +0100
On Wed, Jun 15, 2011 at 1:57 PM, Fredrik Widlund
<fredrik.widlund@qbrick.com> wrote:
> Hi,
>
> Could someone explain if the following is by design:
> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, 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