lua-users home
lua-l archive

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

On Saturday 15 November 2008, Geoff Richards wrote:
> On Fri, Nov 14, 2008 at 03:22:00PM +0100, Florian Weimer wrote:
> > 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

neat, haven't thought of that.  a more general trick would be an 'after' 

function after (a, b)
    local ok, result = pcall (a)
    assert (ok, result)
    return result

and use like this:

after (function()
    -- do whatever you want
end, function ()
    -- close any acquired resources

a similar result to try...finally... on some languages.  the bad part is that 
each function is a different scope, so the resources can't be local to the 
first one, or won't be seen on the second.   maybe there's some trick 
yield()ing the second function just after acquiring resources... hm... sounds 
doable, maybe even worthwhile :-)


Attachment: signature.asc
Description: This is a digitally signed message part.