Thread overview
RAII limitations in D?
Aug 22, 2014
Timothee Cour
Aug 22, 2014
Dicebot
Aug 22, 2014
Timothee Cour
Aug 22, 2014
Jonathan M Davis
Aug 22, 2014
Adam D. Ruppe
Aug 22, 2014
Kagamin
August 22, 2014
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’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.
}

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?


August 22, 2014
http://dlang.org/phobos/std_typecons.html#.RefCounted
August 22, 2014
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.
August 22, 2014
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)


August 22, 2014
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
August 22, 2014
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.