Skip to content

Commit

Permalink
Add destructor docs (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Aug 11, 2022
1 parent 05c343a commit 0bdeade
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion docs/docs/language/constructors-destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ f<int> main() {
The `ctor` method can also be called manually like calling [other methods](./methods).

## Destructors
*Feature to come ...*
You have the option to create a destructor by providing a `dtor` method on a struct. It does not allow any arguments and has no
return type, since it is a procedure. Destructors can be especially useful for de-allocating objects in heap memory, that were
allocated via `malloc()`. Whenever a struct variable goes out of scope somewhere in the program, the compiler searches for a
destructor and calls it if available.

Here is an example for a custom destructor:

```spice
// Declarations of the generic type T as well as malloc() and free()
type ExampleStruct<T> struct {
string message
T* messageObject
}
p ExampleStruct.dtor() {
free(this.messageObject);
}
```

0 comments on commit 0bdeade

Please sign in to comment.