aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 19:56:53 +0330
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 21:24:34 +0330
commita3450d687628661aa0316cfc995934f6ce0c4e90 (patch)
tree75ed354e2c274317df25e3dcdd9ab5ea72dff2f2 /docs
parenttodo: run.py's sieve checksum fails on main (diff)
defer: any single statement (if/else, loops, switch, unchecked, unbounded), not just an expression or a block
Diffstat (limited to 'docs')
-rw-r--r--docs/LEARN.md2
-rw-r--r--docs/SPEC.md2
2 files changed, 2 insertions, 2 deletions
diff --git a/docs/LEARN.md b/docs/LEARN.md
index e9cdbcf..fd399e3 100644
--- a/docs/LEARN.md
+++ b/docs/LEARN.md
@@ -176,7 +176,7 @@ output:
### defer
-`defer expr;` or `defer { statements }` runs code when the enclosing block ends: after the last statement, and also on `return`, `break` and `continue` out of it. defers in a block run last registered first. the deferred code is compiled at each exit, so it sees the variables as they are when it runs, and `return e;` computes `e` before any defer runs. `defer rt.free(p);` hands the owner `p` over at that point: `p` stays usable until the block ends and is not freed twice. `return`, `break` and `continue` are not allowed inside a deferred statement.
+`defer expr;`, `defer { statements }` or any other single statement (`defer if (c) a; else b;`, a loop, a `switch`) runs code when the enclosing block ends: after the last statement, and also on `return`, `break` and `continue` out of it. defers in a block run last registered first. the deferred code is compiled at each exit, so it sees the variables as they are when it runs, and `return e;` computes `e` before any defer runs. `defer rt.free(p);` hands the owner `p` over at that point: `p` stays usable until the block ends and is not freed twice. `return`, `break` and `continue` are not allowed inside a deferred statement.
[example source](../examples/learn/34-defer.zc):
diff --git a/docs/SPEC.md b/docs/SPEC.md
index 32afdfb..aad7ff6 100644
--- a/docs/SPEC.md
+++ b/docs/SPEC.md
@@ -640,7 +640,7 @@ int y = 7; asm { y @ 1 + y ! } // y is 8
### 4.14 defer
-4.14.1 `defer expr;` and `defer { statements }` register a statement that runs when the block that contains the `defer` ends. the tokens are kept, not compiled, at the `defer`; the compiler compiles them again, in place, at every point where control leaves the block: the end of the block, and a `return`, `break` or `continue` that leaves it. (a function body is a block; so are a loop or `if` body, a `switch` case and a block expression.) the code is therefore duplicated at each exit.
+4.14.1 `defer` followed by one statement (`defer expr;`, `defer { statements }`, `defer if (c) a; else b;`, a loop, a `switch`, or an `unchecked` or `unbounded` statement) registers that statement; it runs when the block that contains the `defer` ends. the tokens are kept, not compiled, at the `defer`; the compiler compiles them again, in place, at every point where control leaves the block: the end of the block, and a `return`, `break` or `continue` that leaves it. (a function body is a block; so are a loop or `if` body, a `switch` case and a block expression.) the code is therefore duplicated at each exit.
4.14.2 several defers in one block run in reverse order of registration. an early exit runs the defers of every block it leaves, innermost block first, and within a block newest first. `return` runs those of the whole function, `break` and `continue` only those inside the loop or switch they leave.