Thoughts from newcommer

Basile B. via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 17 05:39:56 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.
>
> 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.


D would have the ability to have a nice container that would do 
RAII (for classes since for structs, __dtors are called 
automatically) but such a thing doesn't exist in the standard 
library. For example in Delphi or FPC standard libs there's the 
TObjectList class that can be set to delete the instances stored 
in the list (but there's still the obligation to explicitly free 
the list itself). D could do even better:

An interesting fact: for the members that are plain structs (not 
new-ed struct), __fieldDtor automatically calls the __xdtor of 
the structs. __fieldDtor  is itself called automatically, so

- make a `struct ObjectContainer {}` (basically it would be an 
array)
- in this ObjectContainer, add a member function that adds new 
"owned" resources (e.g `T addNew(T,A...)(A a){items ~= 
make!T(Mallocator.instance, a); return items.last;}`)
- in the ObjectContainer `~this()`, add code that loop the 
resources and dispose() them.

Resources will be automatically freed:

class/struct Stuff
{
     ObjectContainer resources;
}

when a Stuff goes out of scope or is released, this happens:

Stuff.__fieldDtor() -> ObjectContainer.__xdtor() -> foreach(r; 
resources) r.__xdtor.

So there's definitively the features to do RAII in D but they are 
not usable "out-of-the-box", one has to code his specialized 
containers.

> scope(exit) is a much cleaner solution than a finally clause, 
> but not as clean as RAII.

Actually for local variables and other temporaries I personally 
don't care much about RAII. I even think that in this case I can 
let the GC managing the junks.

> If the language supports RAII, how come people are not using it?

Because the default management is done by the GC and the GC 
works. Mixing RAII and GC can lead to unexpected results. Put a 
GC-managed member (e.g a string) in a RAII-managed aggregate and 
then, at a time, the GC will think that the member is not used 
anymore and it frees it.

___
note about the ObjectContainer: it can be very easy to do 
actually, using std.array.Array and "alias this". No need to code 
a full container.


More information about the Digitalmars-d mailing list