slight correction for exception safety?
Unknown W. Brackets
unknown at simplemachines.org
Sun Oct 15 16:42:21 PDT 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?
More information about the Digitalmars-d
mailing list