What is the idiomatic way of handling variational values ​​in variable assignment?

I have code that wants to get some additional return values ​​from a function and pass them forward last by:

local ok, ... = coroutine.resume(co)
do_stuff(ok)
return ...

However, this will not be performed, since ...in assigning a variable is a syntax error.

I could get around this limitation using the old "function arguments and equivalent variables" trick and function call immediately

return (function(ok, ...)
    do_stuff(ok)
    return ...
)(coroutine.resume(co))

but I think that would not be very idiomatic or effective. Are there any smarter ways to solve this problem of handling the remaining values ​​returned by the call resume?

EDIT: By the way, this is necessary to work with nilvalues ​​in extra arguments

EDIT2: , .

+5
2

IMHO, - vararg , .
- " ":

-- Lua 5.2 only
local t = table.pack(coroutine.resume(co))
do_stuff(t[1])
return table.unpack(t, 2, t.n)
+5

- :

local t = { coroutine.resume(co) }
do_stuff(table.remove(t, 1)) 
return unpack(t)  -- table.unpack(t) in lua 5.2

, , , , , .

+2

All Articles