Defer¶
defer runs a block of code when the current scope exits, whether normally or via early return.
Syntax¶
Use Cases¶
Resource cleanup:
craft process_file(path: str) -> void {
make f = fs.open_file(path, "r");
defer { fs.close(f); }
make content = fs.read_all(f);
print(content);
send;
}
Unlock mutex (when sync primitives exist):
Temporary state:
Execution Order¶
Multiple defer blocks in the same scope run in reverse order (last deferred runs first):