lua-users home
lua-l archive

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


This is not a new idea (see [1]) but we have an opportunity to do
something interesting with 5.2 lexical scoped blocks. If leaving a
block (by explicit return, finishing, or even error) can trigger a
metamethod on the environment, then a lot of interesting applications
are possible. (I think Mark H may have mentioned this in passing.)

One could ensure that a file was immediately closed, resources
released, etc, and generally implement the RAII pattern [2]

It should be relatively easy to emit code to cover normal block exit,
although errors are awkward. This would of course have to be
efficiently done not to slow down other uses of lexically scoped
blocks.

steve d.

[1] http://lua-users.org/lists/lua-l/2006-09/msg00846.html
[2] http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization