lua-users home
lua-l archive

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


On Sat, Nov 15, 2008 at 3:40 PM, Javier Guerra Giraldez <javier@guerrag.com> wrote:
On Saturday 15 November 2008, Geoff Richards wrote:

> 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'
wrapper:

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

and use like this:

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

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 :-)


--
Javier

You can always use do...end blocks to scope things the way you like it.

do

  -- Set up shared resources here...
  local this, that, etc

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

end