lua-users home
lua-l archive

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


> How do you return multiple values from a function that pcalls a function 
> that returns an unknown number of values possibly including nil in a 
> manner that doesn't involve undefined behaviour?
>
> [...]
>
> function b()
>   local status, r = pcall(function() return { a() } end)
>   if not r then error("nicely handled error!") end
>   return unpack(r)
> end
>
> [...]


function pack (...)
  return { n = select("#", ...); ... }
end

function b()
  local r = pack(pcall(function() return { a() } end))
  if not r[1] then error("nicely handled error!") end
  return unpack(r, 2, r.n)
end

?


-- Roberto