lua-users home
lua-l archive

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




On 31/05/16 09:34 AM, Peter Melnichenko wrote:
On Tue, May 31, 2016 at 3:11 PM, steve donovan
<steve.j.donovan@gmail.com> wrote:
I tend to side with Jonne here - exceptions are overused. It's easy to
check if a file exists, why throw an exception to be handled dozens of
stack frames down? (--> Java <---)
Because I may not want to write

     local result, err = func()
     if not result then return nil, err end

dozens of times.

(Sorry, as expected, this thread becomes the usual "errors vs,
exception" debate)

-- Peter

If Lua had block expressions and macros, you'd be able to do something like

local retv = try!(func()) --> local retv = do local result, err = func() if not result then return nil, err end end with result

where `do ... end with <local>` would be "block as expression" syntax.

Yes, I know there are ways to run code in an expression as if it was a block (`<expr> or true and <expr> ...` comes to mind), but you currently cannot use variables or return (a return() function could solve the latter). There's also `(function() end)()` but, as that is an anonymous function, any use of `return` would return from the closure itself, again defeating the purpose.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.