Suppose I have two Lua arrays a = {1, 2, 3}
and b = {4, 5, 6}
.
And then do the following assignation
q, w, e, r, t, y = table.unpack(a), table.unpack(b)
I would expect to get that
q == 1 and w == 2 and e == 3 and r == 4 and t == 5 and y == 6
But I get that
q == 1 and w == 4 and e == 5 and r == 6
and t, y
are nil
!
Could you please explain such a strange behavior?
Suppose I have two Lua arrays a = {1, 2, 3}
and b = {4, 5, 6}
.
And then do the following assignation
q, w, e, r, t, y = table.unpack(a), table.unpack(b)
I would expect to get that
q == 1 and w == 2 and e == 3 and r == 4 and t == 5 and y == 6
But I get that
q == 1 and w == 4 and e == 5 and r == 6
and t, y
are nil
!
Could you please explain such a strange behavior?
From the Lua documentation on multiple results:
Lua always adjusts the number of results from a function to the circumstances of the call. When we call a function as a statement, Lua discards all of its results. When we use a call as an expression, Lua keeps only the first result. We get all results only when the call is the last (or the only) expression in a list of expressions.
So table.unpack(a), table.unpack(b)
doesn't concatenate all return values from both calls. You get the first return value of table.unpack(a)
, followed by all return values of table.unpack(b)
.