Thoughts from newcommer

Stanislav Blinov via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 17 06:10:29 PDT 2017


On Monday, 17 April 2017 at 11:14:47 UTC, Shachar Shemesh wrote:
> On Sunday, 16 April 2017 at 17:00:25 UTC, Jack Stouffer wrote:
>
>> So we can say that D has buggy RAII. Claiming that D doesn't 
>> have RAII is equally false.
>
> To me, that's a basic misunderstanding of what RAII mean. RAII, 
> to me, means you wrap your resource in a container, and then 
> can just go ahead and forget about releasing it. As such, 
> saying that D has RAII, but they only work most of the time 
> means that the core functionality that RAII provides is simply 
> not working.

RAII !== containers. Containers will not release resources for 
types that are themselves don't do so via their destructor. You 
can have a vector<FILE*>, and it will dutifully release it's own 
memory, but won't close the files.

Containers are library types. RAII is a language feature. How on 
Earth in this discussion the two became interchanged?

> There are other languages out there that have unreliable 
> destructors. Java has them. Python has them. No one will use 
> them.
>
> D's destructors are in much better shape than either Java's or 
> Python's, but let's take a simple survey: Does anyone here use 
> RAII in D? Please step forward.

I do. Phobos does. vibe.d does. Many other projects do.

> Please go over your scope(exit)s and tell me that 70% of them 
> (and I'm being generous here, I'm guessing actual number is 
> closer to 95%) are *not* release of resources acquired in the 
> same scope. If they are, then you are in a position where RAII 
> would have made your code cleaner and simpler, but you chose 
> not to use it.

Writing a whole new struct just to release one specific resource 
once is neither cleaner nor simpler.

> scope(exit) is a much cleaner solution than a finally clause, 
> but not as clean as RAII. If the language supports RAII, how 
> come people are not using it?

If it is more convenient to use scope(exit) than to introduce a 
new type, I don't see why the latter should be considered. At 
least until the need for the same scope(exit) is encountered more 
than once. Not to mention there are also scope(success|failure) 
that simply cannot be achieved with destructors.

D supports ("has") RAII idiom, period. Whether or how it is used 
by various libraries does not in any way augment that fact.


More information about the Digitalmars-d mailing list