lua-users home
lua-l archive

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


On 5/11/2009, at 9:07 AM, Florian Weimer wrote:
* 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

Nice! Thanks Florian! And excuse me for putting r where I meant status.

Cheers,
Geoff