lua-users home
lua-l archive

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


On Aug 30, 2014, at 6:03 AM, Andrew Starks <andrew.starks@trms.com> wrote:

> function o.method(self, fun, ...)
> 
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
> 
> end

So, as Tim has pointed out, table.pack is what you want to make this work.

Something along these lines:

function Try( aFunction, ... )
  local aResult = table.pack( pcall( aFunction, ... ) )
  local ok = aResult[ 1 ]
  if ok then
    return table.unpack( aResult, 2, aResult.n )
  end
end

If you feel adventurous, you could easily build a simple try/catch/finally mechanism out of it.

On the other hand, perhaps this was a purely rhetorical question?!?