Recommended way to do RAII cleanly

Jonathan M Davis jmdavisprog at gmail.com
Mon Jul 12 18:23:35 PDT 2010


On Monday, July 12, 2010 18:15:04 Nick Sabalausky wrote:
> "torhu" <no at spam.invalid> wrote in message
> news:i1ft84$2h4j$1 at digitalmars.com...
> 
> > I think the conclusion is that RAII is less important in D than in C++.
> > In D you use scope (exit), or even finally, like in Java or Python. The
> > other use of scope, as a storage class, is supposed to go away, and I
> > suspect I'm not the only one who's going to miss it.
> 
> As good as scope guards are, anytime you have an object that has some sort
> of cleanup function that needs to be called when you're done with it, it's
> absurd to think that requiring the *user* of the object to use scope guards
> *by convention* is ever a satisfactory substitute for real RAII.
> 
> And I have to say, I'm rather disappointed to hear that scope objects are
> going away. What is the reason for that?

IIRC, Walter was of the opinion that you could do the same with structs without 
needing scope. And as long as you can default construct them or your RAII object 
requires arguments to its constructor, that's more or less true (if arguably 
limiting). The question, therefore is whether there is a means to effectively 
default construct structs which is not going to be deprecated. I'd still like 
scope to stick around (even if all it did was put the class on the heap as 
normal and called clear() on it when it left scope rather than putting it on the 
stack like I believe it does at the moment), but if structs can totally take 
care of the RAII issue, then it's not as bad. But the situation with structs and 
default constructors is a bit frustrating.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list