lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, 2010-12-20 at 21:23 +0800, starwing wrote:
> 2010/12/20 Michal Kottman <k0mpjut0r@gmail.com>:
> but what if I don't know the amount of multi return values?

Then you either have to use the table ('th' is the thread, '...' is the
vararg expression):

local ret = {coroutine.resume(th)}
local ok = table.remove(ret, 1) -- returns 1st element and shifts other
if ok then
  -- 'ret' contains the return values
  -- you can use unpack(ret) to get multi-return values again
else
  error(ret[1])
end

Or you can use a helper function:

local function process(ok, ...)
  if ok then
    -- work with '...' as normal
  else
    error(...)
  end
end
process(coroutine.resume(th))