lua-users home
lua-l archive

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



> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Daniel Silverstone
> Sent: dinsdag 28 april 2015 12:33
> To: lua-l@lists.lua.org
> Subject: Re: clumsiness of pcall() syntax and how we deal with it
> 
> On Tue, Apr 28, 2015 at 13:19:04 +0300, Konstantin Osipov wrote:
> > The first problem is in ambiguity of the second argument.
> > In case there is no error, it's the return value of the function.
> > Otherwise, it's a string with a message.
> 
> I tend to use 'ok, result = pcall(xyz, blah)' and then:
> 
> if ok then
>   ...use result as useful result...
> else
>   ...result is the error...
> end
> 
> > How to name the second argument? And what if a function returns
> > multiple arguments?
> 
> Multiple returns is a toughie and I will admit I tend not to use
> them in cases where I want to return errors too.  Then again I tend
> to code functions which return an ok, result tuple most of the time
> and then deal with wrappering them in assert() when I'd rather they
> threw an error.
> 

After recently having worked on http clients (with copas/luasocket/luasec) I have started to appreciate the luasocket functions protect, try and newtry for error handling.

Thijs