why scope(success)?
Walter Bright
newshound at digitalmars.com
Fri May 12 11:16:52 PDT 2006
Ben Hinkle wrote:
> I hope this doesn't come of as a flame, but I'm wondering if anyone is using
> scope(success) and why. I can't find any reason for it.
>
> Some background: I've slowed my D work to focus on some C experimental
> features I'm calling Cx: http://www.tinycx.org and currently I'm
> implementing the error handling using reserved labels "error:" and
> "finally:". The error label is roughly like scope(failure) and the finally
> label is roughly like scope(exit). There's no try-catch-finally. I don't
> plan on adding anything like scope(success) because I couldn't think of why
> anyone would want to use it. Why not just put the code at the end of the
> scope like normal code-flow? I suppose one could code the entire scope in
> reverse just for kicks:
> void main() {
> scope(success) printf("world\n");
> scope(success) printf("hello ");
> }
The scope(success) will execute even if the scope is exited via a
return, break, or continue statement, but not if it is exited via a
thrown exception. Thus, it is fundamentally different from just putting
the code at the closing }.
The code:
{
foo();
scope(success) bar();
def();
}
is equivalent to:
{
foo();
int x;
try
{
x = 0;
def();
}
catch (Object o)
{
x = 1;
throw o;
}
finally
{
if (x == 0)
bar();
}
}
which is a tedious, error-prone, and unobvious thing to write.
More information about the Digitalmars-d
mailing list