Re: RAII limitations in D?

2014-08-22 Thread Russel Winder via Digitalmars-d-learn
On Thu, 2014-08-21 at 19:22 -0700, Timothee Cour via Digitalmars-d-learn
wrote:
 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.
 }

The author has clearly not actually used Python. There is nothing that C
++ can do with RAII that you cannot do with Python/context managers/with
statement. Nothing.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



RAII limitations in D?

2014-08-21 Thread Timothee Cour via Digitalmars-d-learn
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?


Re: RAII limitations in D?

2014-08-21 Thread Dicebot via Digitalmars-d-learn

http://dlang.org/phobos/std_typecons.html#.RefCounted


Re: RAII limitations in D?

2014-08-21 Thread Adam D. Ruppe via Digitalmars-d-learn
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.


Re: RAII limitations in D?

2014-08-21 Thread Timothee Cour via Digitalmars-d-learn
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)


Re: RAII limitations in D?

2014-08-21 Thread Jonathan M Davis via Digitalmars-d-learn
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


Re: RAII limitations in D?

2014-08-21 Thread Kagamin via Digitalmars-d-learn
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.