[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How do you return multiple values?
- From: Florian Weimer <fw@...>
- Date: Wed, 04 Nov 2009 21:07:08 +0100
* Geoff Leyland:
> function a()
> -- could cause an error, could return different numbers of values
> return 1, nil, 2
> end
>
> function b()
> local status, r = pcall(function() return { a() } end)
> if not r then error("nicely handled error!") end
> return unpack(r)
> end
I guess you should use a helper function and a tail call, like this:
function a()
-- could cause an error, could return different numbers of values
return 1, nil, 2
end
do
local function b1(status, r, ...)
if not r then error("nicely handled error!") end -- ??? status?
return r, ...
end
function b()
return b1(pcall(a))
end
end