Scope guards

Paul Backus snarwin at gmail.com
Mon Jun 26 17:41:16 UTC 2023


On Saturday, 24 June 2023 at 17:00:36 UTC, Cecil Ward wrote:
> I would like to use scope guards but in the guard I need to get 
> access to some local variables at the end of the routine. This 
> doesn’t really seem to make sense as to how it would work, 
> because their values depend on the exact point where the scope 
> guard is called at in the last, exiting line(s) of the routine. 
> Am I misunderstanding?

Scope guards are syntax sugar for try/catch/finally. For example, 
when you write

     auto f = open("foo");
     scope(exit) close(f);
     doSomethingWith(f);

...it gets transformed by the compiler into

     auto f = open("foo");
     try {
         doSomethingWith(f);
     } finally {
         close(f);
     }

I do not understand exactly what the problem is you are having, 
but hopefully this lets you figure out how to solve it.


More information about the Digitalmars-d-learn mailing list