Reputation: 19035
function test() return 1, 2 end
function foo()
return test(), 3
end
Expected result: 1, 2, 3
Actual result: 1, 3
LuaJIT 2.1.0
Upvotes: 0
Views: 1981
Reputation: 1525
@aleclarson answers this question in order to solve this, i want to elaborate why your approach does not work. In lua, multiple return values are possible as are multiple assignments. If you write
local a, b = 1, 2
this works fine. If you omit any requested values, you will get nil
(this also happens in arguments to functions). Now functions can return multiple values, which can be mixed with variables in statements like the above. This then looks like your code:
local a, b, c = f(), 3
This actually truncates the results of f() to one result, takes the 3
as second value and adjusts with a nil
, which gets passed to c
.
See the lua manual for this.
Another way to solve this is to place the function call at the end:
local c, a, b = 3, f()
which admittedly looks a bit strange with the reversed variables order.
Upvotes: 3
Reputation: 19035
function foo()
local a, b = test()
return a, b, 3
end
If you don't know how many return values there are:
function foo()
local tuple = { test() }
table.insert(tuple, 3)
return unpack(tuple)
end
Upvotes: 2