www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - RAII limitations in D?

reply Timothee Cour via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
What would be a good answer to this article?
http://swiftcoder.wordpress.com/2009/02/18/raii-why-is-it-unique-to-c/

Especially the part mentioning D:{
D=E2=80=99s scope keyword, Python=E2=80=99s with statement and C#=E2=80=99s=
 using declaration all
provide limited RAII, by allowing resources to have a scoped lifetime, but
none of them readily or cleanly support the clever tricks allowed by C++=E2=
=80=99s
combination of smart pointers and RAII, such as returning handles from
functions, multiple handles in the same scope, or handles held by multiple
clients.
}

This morning I was pointing to some deprecated usage of scope mentioned in
docs (EMAIL:scope classes mentioned in tutorials, but deprecated). The pull
request (https://github.com/D-Programming-Language/dlang.org/pull/637/files=
)
mentions using struct or classes allocated on the stack via
typecons.scoped. However what about the RAII usage mentioned in the above
article that allows C++ to return handles for eg (impossible via scope),
that get deterministically destroyed?
Aug 21 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
http://dlang.org/phobos/std_typecons.html#.RefCounted
Aug 21 2014
parent reply Timothee Cour via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, Aug 21, 2014 at 7:26 PM, Dicebot via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> wrote:

 http://dlang.org/phobos/std_typecons.html#.RefCounted
That doesn't work with classes though; is there any way to get a ref counted class? (and btw RefCounted should definitely appear in http://dlang.org/cpptod.html#raii)
Aug 21 2014
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, 22 August 2014 at 03:00:01 UTC, Timothee Cour via 
Digitalmars-d-learn wrote:
 On Thu, Aug 21, 2014 at 7:26 PM, Dicebot via 
 Digitalmars-d-learn <
 digitalmars-d-learn puremagic.com> wrote:

 http://dlang.org/phobos/std_typecons.html#.RefCounted
That doesn't work with classes though; is there any way to get a ref counted class? (and btw RefCounted should definitely appear in http://dlang.org/cpptod.html#raii)
It can be made to work with classes and probably should be. There's no fundamental reason why it can't. It's probably just more complicated. - Jonathan M Davis
Aug 21 2014
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 22 August 2014 at 02:22:16 UTC, Timothee Cour via 
Digitalmars-d-learn wrote:
 What would be a good answer to this article?
It's own publication date: Feb 2009. The D struct has changed a lot since then, getting new features ( disable, postblit, more reliable destructor) and bugs notwithstanding is pretty well on point nowadays. All the stuff mentioned in there works now.
Aug 21 2014
prev sibling parent "Kagamin" <spam here.lot> writes:
On Friday, 22 August 2014 at 02:22:16 UTC, Timothee Cour via 
Digitalmars-d-learn wrote:
 Especially the part mentioning D:{
 D’s scope keyword, Python’s with statement and C#’s using 
 declaration all
 provide limited RAII, by allowing resources to have a scoped 
 lifetime, but
 none of them readily or cleanly support the clever tricks 
 allowed by C++’s
 combination of smart pointers and RAII, such as returning 
 handles from
 functions, multiple handles in the same scope, or handles held 
 by multiple
 clients.
 }
Even for C# those are not really problems. Returning from functions is not a problem: you just return it and that's it, because resource management is decoupled from types, the types have no RAII semantics and you can move them around however you want. On the other hand, in C# it's very easy to declare a resource, while in C++ you would need to learn all the black magic of RAII before you can declare a RAII type. Multiple handles in the same scope are not frequent, nested if's cause much more trouble. Handles held by multiple clients are even more rare, and it's usually easy to figure out lifetime for non-memory resources.
Aug 21 2014