Delete statement
The delete statement is used to deallocate (free) an object which was allocated using a custom allocator. If the object was not allocated using a custom allocator (e.g. using stack or wasm GC heap allocation), then the statement has no effect.
Runtime semantics
The delete statement first calls the destructor of the object being deleted. The destructor can be set using the [Destructor] attribute on a class method which has one parameter (the object being destroyed), and returns an i32, which declares whether the object was destroyed successfully. If no destructor method is present, then this step is skipped.
After the destructor finishes, the memory associated with the object is deallocated. This is done by calling the object allocator's class method which has the [AllocatorDeallocate] attribute. If no such method exists, then this step is skipped.
The delete statement yields a single i32 value which is 1 if both the destructor and the deallocator finished correctly, and 0 if either the destructor or deallocator failed. The semantics of failure here are entirely user-defined and can be customised by modifying the destructor or deallocation methods to e.g. fail if the same memory address is deallocated twice.
Example
The following example defines an example allocator with an [AllocatorDeallocate] method, and an example class with the [Destructor] method. Then, an object is created, and destroyed with the delete keyword.