www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D plan to do RAII(Resource Acquisition Is Initialization)?

reply "FrankLike" <1150015857 qq.com> writes:
RAII(Resource Acquisition Is Initialization) is a good thing,will 
D plan to do it?
Feb 21 2015
next sibling parent "weaselcat" <weaselcat gmail.com> writes:
On Saturday, 21 February 2015 at 10:06:26 UTC, FrankLike wrote:
 RAII(Resource Acquisition Is Initialization) is a good 
 thing,will D plan to do it?
RAII in D is handled by structs. It becomes a bit tricky when you want class semantics with RAII, but can be handled by wrapping the class in a struct I suppose. The unique and refcounted implementations would help with this, but they are lacking.
Feb 21 2015
prev sibling parent reply "Olivier Pisano" <olivier.pisano laposte.net> writes:
On Saturday, 21 February 2015 at 10:06:26 UTC, FrankLike wrote:
 RAII(Resource Acquisition Is Initialization) is a good 
 thing,will D plan to do it?
It's already here : import std.stdio; struct Test { ~this() { writeln("RAII"); } } void main() { Test t; // prints "RAII" when goes out of scope }
Feb 21 2015
next sibling parent "Baz" <bb.temp gmx.com> writes:
On Saturday, 21 February 2015 at 18:30:18 UTC, Olivier Pisano 
wrote:
 On Saturday, 21 February 2015 at 10:06:26 UTC, FrankLike wrote:
 RAII(Resource Acquisition Is Initialization) is a good 
 thing,will D plan to do it?
It's already here : import std.stdio; struct Test { ~this() { writeln("RAII"); } } void main() { Test t; // prints "RAII" when goes out of scope }
Feb 21 2015
prev sibling parent "Baz" <bb.temp gmx.com> writes:
On Saturday, 21 February 2015 at 18:30:18 UTC, Olivier Pisano 
wrote:
 On Saturday, 21 February 2015 at 10:06:26 UTC, FrankLike wrote:
 RAII(Resource Acquisition Is Initialization) is a good 
 thing,will D plan to do it?
It's already here : import std.stdio; struct Test { ~this() { writeln("RAII"); } } void main() { Test t; // prints "RAII" when goes out of scope }
He's maybe talking about reference counting because "owning" the resources is very faisable and simple (non gc objects are constructed in this() and destructed in ~this(), but the problem happends when you escape the owned reference as a raw pointer. At a time it may become dangling or even freed by another entity, e.g not the one which has initialized the thing. The first reply is clearly about this (when weaselcat said "It becomes a bit tricky when you want class semantics with RAII"), because std RC is not for classes.
Feb 21 2015