scope(exit) considered harmful

Justin Johansson no at spam.com
Sun Nov 8 11:32:07 PST 2009


Bill Baxter Wrote:

> On Sun, Nov 8, 2009 at 10:44 AM, Justin Johansson <no at spam.com> wrote:
> 
> > Can you understand my frustration?
> 
> Yes, I can certainly understand that it's difficult to use scope()
> constructs when you haven't understood what a scope is.  But that's
> not much of an argument against them.
> 
> The same rant you just wrote would apply equally to C++ RAII objects.
> They won't work properly either if you haven't understood what this
> means: "destructors are called upon exiting the scope in which
> declared".
> 
> --bb

True you have to understand a tool to use it properly.  In the C++ RAII
situation, I would have spotted my mistake straight away.

// C++

if (true) {
  Foo foo;
}

Error foo goes out of scope when if block is exited.

Acknowledge this is what happens with D's scope(exit)
as well though to me it doesn't seem as intuitive as
its C++ counterpart.


Just thinking about how C's atexit() function reflects
"process-scope", I'm just wondering if it would be a
good idea if the D scope statement could be extended
to other types of scope, function scope in particular and
not just curly-brace block scope.  What do you think?

Justin






More information about the Digitalmars-d mailing list