How do I call a context objects destructor or some type of exit() function automatically upon exiting a with {} scope?
Daniel
Daniel
Tue Apr 1 23:00:47 UTC 2025
On Tuesday, 1 April 2025 at 17:33:02 UTC, Ali Çehreli wrote:
> On 4/1/25 10:08 AM, Mike Parker wrote:
> > ```
> > scope ctx = new Context;
> > ```
> >
> > This will allocate the class instance on the stack so the
> destructor
> > will be called when the scope exits.
>
> Another option is to call the destructor explicitly through
> destroy():
>
> import std.stdio;
>
> class C {
> ~this() {
> writeln("Goodbye");
> }
> }
>
> void foo() {
> auto c = new C();
>
> scope (exit) {
> destroy(c); // <-- HERE
> }
> }
>
> void main() {
> writeln("Calling foo");
> foo();
> writeln("Exiting main");
> }
>
> Ali
Thanks! I will try this scope(exit) trick in order to pop the
context internally, these contexts hold local variable
definitions so of course I would want to pop them.
More information about the Digitalmars-d-learn
mailing list