[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: block-scope finalization
- From: Philipp Janda <siffiejoe@...>
- Date: Mon, 23 Nov 2015 22:25:57 +0100
Am 23.11.2015 um 21:09 schröbte Javier Guerra Giraldez:
On Mon, Nov 23, 2015 at 2:38 PM, Coda Highland <chighland@gmail.com> 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, ... )
after()
if ok then
return ...
else
error( (...), 0 )
end
end
function finally( main, after )
return _finally( after, pcall( main ) )
end
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.
Philipp
- References:
- block-scope finalization, Viacheslav Usov
- Re: block-scope finalization, Soni L.
- Re: block-scope finalization, Viacheslav Usov
- Re: block-scope finalization, Philipp Janda
- Re: block-scope finalization, Viacheslav Usov
- Re: block-scope finalization, Philipp Janda
- Re: block-scope finalization, Viacheslav Usov
- Re: block-scope finalization, Philipp Janda
- Re: block-scope finalization, Philipp Janda
- Re: block-scope finalization, Viacheslav Usov
- Re: block-scope finalization, Coda Highland
- Re: block-scope finalization, Javier Guerra Giraldez