digitalmars.D - slight correction for exception safety?
- Karen Lanrap (17/17) Oct 15 2006 From the article:
- Ivan Senji (3/5) Oct 15 2006 Arguments to functions are expressions and not statements, so this
- Unknown W. Brackets (26/47) Oct 15 2006 In the specific case named, I'm not sure it matters; is an exception
From the article:
class Foo
{
bool verbose; // true means print messages, false means
silence
...
bar()
{
auto verbose_save = verbose;
verbose = false;
scope(exit) verbose = verbose_save;
...lots of code...
}
}
The scope statement should be moved before the assignment.
By the way: what will happen if a scope statement is used as an
argument for a lazy parameter of a function?
Oct 15 2006
Karen Lanrap wrote:By the way: what will happen if a scope statement is used as an argument for a lazy parameter of a function?Arguments to functions are expressions and not statements, so this cannot happen.
Oct 15 2006
In the specific case named, I'm not sure it matters; is an exception
really going to happen between those two lines? If it could, it's just
as likely one could happen inside the scope statement itself, nay?
I think it would increase clarity to change it as you suggest, but it
was done this way to compare it with the example above it with as little
changes as possible to the code flow (I assume.)
And, actually, bar() should really have a return type - shouldn't it?
At least void, or something.
As for using it as a lazy parameter; it would build a delegate, and so
the code would always be run. Consider:
function foo()
{
function dg()
{
scope (exit)
writefln("Exited dg's scope.");
}
dg();
}
In the above, it is not unclear what will happen. A lazy parameter
simply constructs a delegate, and as such would work exactly the same
way (being that the lazy expression has its own scope.)
I agree that this is not immediately clear. In my opinion, scope
statements should be disallowed in lazily evaluated expressions (if they
aren't already) because of this lack in clarity.
-[Unknown]
From the article:
class Foo
{
bool verbose; // true means print messages, false means
silence
...
bar()
{
auto verbose_save = verbose;
verbose = false;
scope(exit) verbose = verbose_save;
...lots of code...
}
}
The scope statement should be moved before the assignment.
By the way: what will happen if a scope statement is used as an
argument for a lazy parameter of a function?
Oct 15 2006









Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> 