lua-users home
lua-l archive

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



I wonder what all the cost would be? I'm thinking of them as essentially a syntax sugar for pcall/xpcall, so:
try
    do_things()
catch(ex)
    report_error(ex)
end

As a compulsive parser hacker, this is something I've certainly thought about doing.  But, I'm actually not convinced it's worth the trouble.  If all you want it try/catch, I feel like function wrapping is generally sufficient, for example:

  do_things_safely = error_reporter( function() 
    return do_things() 
   end)

I have written a tiny piece of sugar to simplify the above, specifically, a 'pipe' operator, so I can avoid the parens, and just write:

  do_things_safely = error_reporter | function() 
    return do_things() 
   end

But that's a long way from a try/catch syntax.

The Lua exception handing feature I'd really like would be something like go's "defer" command.  But there I've decided I'm content to just write resource wrappers in C++.  I.e., if I have some sort of resource I want to access in an exception safe way, I'll write a small C++ helper that opens the resource, closes it in a destructor, and then calls a Lua function while the resource is in scope.  And that works fine as long as as Lua is compiled as C++, and LUAI_THROW is defined appropriately.

Getting a go-like "defer" into Lua might be doable, but, it would complicate Lua's wonderful efficient execution loop.  Piggybacking on C++ isn't really that inconvenient; and it's avoids muddying up Lua with all the complexities of an exception-safe deferred call implementation.

-Sven