How to do conditional scope(exit) ?

John Dougan jdougan at acm.org
Wed Dec 18 22:16:45 UTC 2024


As the subject. The obvious way:

```D
import std;

int test(int val)
{
     writeln("A");
     if (val % 2 == 0)
     {
         writeln("b");
         scope (exit)
             writeln("scope exit ", val);
         writeln("c");
     }
     writeln("F");
     return 2 * val;
}

void main()
{
     writeln("== start =========================");
     int v1 = test(1);
     writeln("== 2 ==============================");
     int v2 = test(2);
     writeln("== end ===========================");
}
```
results in:

```
== start =========================
A
F
== 2 ==============================
A
b
c
scope exit 2
F
== end ===========================
```

which isn't what I want. The case for `test(1)` is fine. For 
`test(2))` I want the  `scope exit 2` to come after the `F`. Is 
there a way to tell it that I want to use an enclosing scope or 
make it ignore the scope on the `if`?

Cheers,
   -- John


More information about the Digitalmars-d-learn mailing list