Calls a function when current frame is being discarded.
defer instruction takes either a function name, or
an operand evaluating to callable value as input.
It has no output, and
is used solely for its side effects.
Deferred calls are invoked when the call frame in which they were registered is being logically discarded, that is:
returninstruction is executed
tailcallinstruction is executed
Deferred calls are guaranteed to finish running before the frame is dropped, and its values destroyed so all pointers to values existing in the frame being dropped that were passed to the deferred call are valid for the duration of the deferred call. This may be, for example, used to implement resource management: a deferred call may receive a pointer to a file handle, and close the handle via the pointer.
Deferred calls are invoked in reverse order of registration.
If deferred call to
fA/0 is registered before
deferred call to fB/0 then
fB/0 will be invoked
Deferred calls may be nested, i.e. functions invoked by
defer instruction may
register further deferred calls.
The order of invocation is preserved in such situations, meaning that if
fA/0 and then
fC/0 then the order of invocations is as follows:
fC/0 is invoked before
fA/0 because it was deferred later.
Hello World! (b) Hello World! (nested) Hello World! (a)
frame, param, pamv, arg, tailcall, return, process, call.