lua-users home
lua-l archive

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

Am 23.11.2015 um 21:09 schröbte Javier Guerra Giraldez:
On Mon, Nov 23, 2015 at 2:38 PM, Coda Highland <> wrote:
Deterministic finalization is a means to a particular end, but what is
that desired end? Ultimately: We want a specific function to be called
when control exits a block, no matter how it exits. In C++, we do this
using block scoping on a local variable. In Python, we have "finally:"
blocks and we have context managers and the "with" keyword; Java has
finally and try-with-parameters for the same.

totally concur with you on this.

It's good enough for most cases and very easy to implement. (Btw., I have fixed that for you:)

    local function _finally( after, ok, ... )
      if ok then
        return ...
        error( (...), 0 )

    function finally( main, after )
      return _finally( after, pcall( main ) )

far too many people say (not only about Lua) something on the lines
of "professional programmers do RAII, therefore static languages are
the only professional tools".

That's nonsense. Many static languages don't have RAII.
Joking aside, so far I have not found a better tool for generic resource management than RAII. Advantages of RAII over finally/using/with: You need only one language feature (destructors) instead of two (finalizers and finally), so the language is less complex/bloated (see C++!). And more importantly you only write your resource management code once and not multiple times scattered around in your source code.