improving scope(finally/success)
Idan Arye via Digitalmars-d
digitalmars-d at puremagic.com
Thu Dec 3 05:07:53 PST 2015
On Thursday, 3 December 2015 at 11:41:29 UTC, Tomer Filiba wrote:
> it'd be really helpful if scope() statements got hold of the
> return value or exception, e.g.,
>
> scope(success, retval) {
> writeln("the retval is", retval)
> }
>
> scope(failure, ex) {
> if(typeid(ex) == typeid(MyException)) {
> callTheCops();
> }
> }
>
> it would make logging very easy, and since these statements are
> basically code-rewrites i don't suppose it would be hard to
> implement. from a syntax point of view:
>
> scope(succes[, VARNAME]) // where VARNAME would be the
> return value (a const tmp variable?)
> scope(failure[, VARNAME]) // where VARNAME would be hold the
> exception (a Throwable)
>
> i mean, code such as
>
> int f() {
> scope(exit) writeln("bye");
> return 5;
> }
>
> is rewritten as something like
>
> int f() {
> try {
> auto tmp = 5;
> }
> finally {
> writeln("bye");
> }
> return tmp;
> }
>
> so `tmp` is already there for the finally clause (modulo
> scoping issues)
int foo(bool cond) { // scope 1
{ // scope 2
scope(success, retval) {
writeln("the retval is", retval)
}
if (cond) { // scope 3
return 1;
}
}
return 2;
}
`foo(true)` should obviously print "the retval is 1", but what
should `foo(false)` print? It's `scope(success)` block should run
when it exists scope 2, but the return value is only determined
at the end of scope 1.
More information about the Digitalmars-d
mailing list