Destructors¶
A destructor is a special method that allows to perform some actions during struct or class instance destruction. It’s needed in order to free some resources that are belong to the object - deallocate memory, close a file, close a network connection, etc.
A destructor is a method with special name destructor
.
It’s possible to avoid specifying of this
parameter, in such case implicit this
will be created.
A destructor must have only single parameter - this
, other parameters are not allowed.
this
in a destructor can’t be declared as byval
.
It’s not possible to overload destructors.
A destructor is called always when a struct or class instance should be destroyed:
- When leaving a scope (for local variables)
- When returning from a function (for arguments)
- In a destructor of other struct/class (for fields)
- During destruction of a temporary variable
- In operations with containers
Destructors for local variables in a single scope and for arguments are called in order reverse to creation order. A destructor of a struct or class calls field destructors implicitly.
struct T {}
struct S
{
fn destructor()
{
} // The "t" destructor will be called
T t;
}
fn Foo( S s0 )
{
var S s1;
var S s2;
{
var S s3;
} // A destructor for "s3" will be called
// Destructors for "s2" and "s1" will be called
} // A destructor for "s0" will be called
Destructors generation¶
If a struct or a class has no explicit destructor it will be generated by the compiler automatically. A generated destructor does nothing except calling destructors of fields.
If there is no necessity no destructor should be defined explicitly.
Explicit destructor existence may prevent a struct to be constexpr
.