[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: block-scope finalization
- From: Coda Highland <chighland@...>
- Date: Mon, 23 Nov 2015 13:46:06 -0800
On Mon, Nov 23, 2015 at 1:25 PM, Philipp Janda <siffiejoe@gmx.net> wrote:
> 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
>
Yeah, I realized I accidentally deleted the pcall() out of my
implementation during an edit. ^^() And the worker function does do a
good job of cleaning up the varargs, so thanks for that, that's
probably way faster (especially in LuaJIT).
>> 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.
That's a fallacious claim. You DO need two language features to
implement RAII -- destructors and deterministic cleanup. Destructors
with nondeterministic cleanup is exactly what Lua has right now with
the __gc metamethod.
Saying that C++ is less complex/bloated is also a pretty flimsy claim,
considering that the typical criticism of C++ is that it's
overengineered and complicated, and that the typical praise of Lua is
its minimalism.
And as for where resource management goes, C++ code involves writing
resource management ALL OVER THE PLACE. You have to pay attention to
scoping and you have to either manually free heap-allocated memory or
explicitly use a container that does it for you. That's a far cry from
keeping the code centralized.
And I say these things from the perspective of a C++ lover! C++ is my
favorite language and until recently I wrote the majority of my code
in it.
/s/ Adam
- 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
- Re: block-scope finalization, Philipp Janda