在变量赋值中处理可变参数值的惯用方法是什么?

hug*_*omg 5 lua

我有一些代码想要从函数中获取一些额外的返回值,然后将它们向前传递:

local ok, ... = coroutine.resume(co)
do_stuff(ok)
return ...
Run Code Online (Sandbox Code Playgroud)

但是,由于...变量赋值是语法错误,因此不会运行.

我可以通过使用旧的"函数参数和变量是等效的"技巧和一个立即调用的函数来解决这个限制

return (function(ok, ...)
    do_stuff(ok)
    return ...
)(coroutine.resume(co))
Run Code Online (Sandbox Code Playgroud)

但我想这样做不会非常惯用或有效率.有没有更合理的方法来解决处理resume呼叫返回的剩余值的问题?

编辑:顺便说一下,这需要使用nil额外参数中的值

EDIT2:看起来像使用立即调用的函数一直是最好的方法.

Ego*_*off 5

恕我直言,最好的方法是将vararg作为参数传递给辅助函数,就像你在问题中所做的那样.
另一种方法是"打包解压":

-- Lua 5.2 only
local t = table.pack(coroutine.resume(co))
do_stuff(t[1])
return table.unpack(t, 2, t.n)
Run Code Online (Sandbox Code Playgroud)