Re: Maybe I found a bug in function unpack.
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Maybe I found a bug in function unpack.
- From: Ezra Sims <ezra.sims@...>
- Date: Tue, 6 Sep 2011 00:09:07 -0400
On Mon, Sep 5, 2011 at 11:42 PM,
<hei.minglei@zte.com.cn> wrote:
Lua 5.1.4 Copyright (C) 1994-2008
Lua.org, PUC-Rio
> a={1,2,3}
> b={9,8,7}
> c={unpack(a),unpack(b)}
> =#c
4
> for _,v in ipairs(c) do print(v)
end
1
9
8
7
>
I think the variable c should be {1,2,3,9,8,7},
doesn't it?
I can understand the confusion, but it is expected behavior. In any given statement, you only get the first return per function until the last in
the chain. Table declarations are no exception. Without that behavior, you could end up with some very awkward situations, like so:
function foo(bar)
return bar*2,bar*3,bar*4
end
t = { foo(7), 15, foo(8) }
for i,v in ipairs(t) do print(i,v) end
-- RETURNS
-- Expected (actual) behavior:
1 14 -- foo(7), return 1
2 15 -- manually assigned
3 16 -- foo(8), return 1
4 24 -- foo(8), return 2
5 32 -- foo(8), return 3
-- What-if scenario
1 14 -- foo(7), return 1
2 21 -- foo(7), return 2
3 28 -- foo(7), return 3
4 15 -- manually assigned
5 16 -- foo(8), return 1
6 24 -- foo(8), return 2
7 32 -- foo(8), return 3
As you can probably see, this could become inconvenient very quickly.