Struct destructor

Matheus m at mail.com
Sat Mar 2 12:42:01 UTC 2019


On Saturday, 2 March 2019 at 11:32:53 UTC, JN wrote:
> ...
> Is this proper behavior? I'd imagine that when doing 
> foos.remove("bar"), Foo goes out of scope and should be 
> immediately cleaned up rather than at the end of the scope? Or 
> am I misunderstanding how should RAII work?

https://dlang.org/spec/struct.html#struct-destructor

"Destructors are called when an object goes out of scope. Their 
purpose is to free up resources owned by the struct object."

Example:

import std.stdio;

struct S
{
     ~this()
     {
         import std.stdio;
         writeln("S is being destructed");
     }
}


void main(){
     {
         S s = S();
         scope (exit)
         {
             writeln("Exiting scope");
         }
     }
     writeln("Ending program.");
}

> Output:
Exiting scope
S is being destructed
Ending program.

Matheus.



More information about the Digitalmars-d-learn mailing list