lua-users home
lua-l archive

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


On Fri, Nov 14, 2008 at 03:22:00PM +0100, Florian Weimer wrote:
> * Javier Guerra Giraldez:
> 
> > in short, just add a 'close' method to the Lua object, and call it
> > as soon as you don't need it.
> 
> Is there a short idiom to express this sort of resource management in
> the presence of exceptions?

You can use the Lisp 'with-open-file' trick.  Write a wrapper function
which handles creating and destroying the value, and a function you supply
to do the actual work on it.  The wrapper can catch any exceptions and
ensure that the object is properly destructed.

I wrote up the most obvious example - closing file handles - on my blog
a while back:

    http://ungwe.org/blog/2008/03/with-open-file/