From 0bdeadee4d1810c2afc26fa6a02fbdfcb95eb9ab Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Thu, 11 Aug 2022 13:49:21 +0200 Subject: [PATCH] Add destructor docs (#186) --- .../docs/language/constructors-destructors.md | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/docs/language/constructors-destructors.md b/docs/docs/language/constructors-destructors.md index 456083f33..ae7cd6614 100644 --- a/docs/docs/language/constructors-destructors.md +++ b/docs/docs/language/constructors-destructors.md @@ -44,4 +44,22 @@ f main() { The `ctor` method can also be called manually like calling [other methods](./methods). ## Destructors -*Feature to come ...* \ No newline at end of file +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 struct { + string message + T* messageObject +} + +p ExampleStruct.dtor() { + free(this.messageObject); +} +``` \ No newline at end of file