| 1 | title: Defer |
| 2 | part: Go |
| 3 | summary: Clean up beside the line that needs it, in the right order, on every way out. |
| 4 | --- |
| 5 | ```raz Defers run |
| 6 | ``` |
| 7 | |
| 8 | `defer s;` runs `s` when the program leaves the statements the `defer` stands in: at their end, or by a `return`, a `break` or a `continue`. Deferred statements run the last first, and only those whose `defer` was reached. So a file is closed beside the line that opens it, however the procedure ends. |
| 9 | |
| 10 | ## The order, and a `return` value |
| 11 | |
| 12 | A `return e;` evaluates `e` before the deferred statements run, so the value is what it was at the return. |
| 13 | |
| 14 | ```raz Order run |
| 15 | module Order; |
| 16 | import Out; |
| 17 | |
| 18 | var counter: INT; |
| 19 | |
| 20 | proc next(): INT; |
| 21 | begin |
| 22 | defer INC(counter); |
| 23 | return counter; (* the old value is returned; then counter grows *) |
| 24 | end; |
| 25 | |
| 26 | proc nest; |
| 27 | begin |
| 28 | defer Out.str("1 "); |
| 29 | defer Out.str("2 "); |
| 30 | defer Out.str("3 "); |
| 31 | Out.str("body: "); |
| 32 | end; |
| 33 | |
| 34 | proc main*; |
| 35 | var a, b: INT; |
| 36 | begin |
| 37 | a := next(); |
| 38 | b := next(); |
| 39 | Out.int(a); Out.char(' '); Out.int(b); Out.char(' '); Out.int(counter); Out.ln; |
| 40 | nest; |
| 41 | Out.ln; |
| 42 | end; |
| 43 | |
| 44 | end Order. |
| 45 | ``` |
| 46 | |
| 47 | ## In a loop |
| 48 | |
| 49 | A `defer` stands in the statement list that holds it, so in a loop body it runs at the end of each pass, and on a `break` or a `continue`. |
| 50 | |
| 51 | ```raz Passes run |
| 52 | module Passes; |
| 53 | import Out; |
| 54 | |
| 55 | proc main*; |
| 56 | var i: INT; |
| 57 | begin |
| 58 | for i := 1 to 4 do |
| 59 | Out.str("pass "); Out.int(i); |
| 60 | defer Out.str(" done\n"); |
| 61 | if i = 3 then break; end; |
| 62 | Out.str(" ..."); |
| 63 | end; |
| 64 | end; |
| 65 | |
| 66 | end Passes. |
| 67 | ``` |
| 68 | |
| 69 | ## Giving back what was taken |
| 70 | |
| 71 | The point is that acquiring and releasing sit next to each other, so a new early `return` added later can't leak. Here an arena and a buffer are both released on every path. |
| 72 | |
| 73 | ```raz Report run |
| 74 | module Report; |
| 75 | import Out, Buf, Heap; |
| 76 | |
| 77 | (* a line, built in a buffer; false (nothing written) if n is negative *) |
| 78 | proc line(n: INT): BOOL; |
| 79 | var b: Buf.Buf; |
| 80 | begin |
| 81 | if n < 0 then return false; end; |
| 82 | Buf.init(b, Heap.heap); |
| 83 | defer Buf.done(b); (* freed on every way out below *) |
| 84 | Buf.str(b, "n is "); |
| 85 | if n > 9 then |
| 86 | Buf.str(b, "large"); |
| 87 | Out.str(Buf.bytes(b)); Out.ln; |
| 88 | return true; |
| 89 | end; |
| 90 | Buf.str(b, "small"); |
| 91 | Out.str(Buf.bytes(b)); Out.ln; |
| 92 | return true; |
| 93 | end; |
| 94 | |
| 95 | proc main*; |
| 96 | begin |
| 97 | _ := line(3); |
| 98 | _ := line(30); |
| 99 | if not line(-1) then Out.str("rejected\n"); end; |
| 100 | end; |
| 101 | |
| 102 | end Report. |
| 103 | ``` |
| 104 | |
| 105 | ## What defer does not do |
| 106 | |
| 107 | A deferred statement can't contain a `return`, `break` or `defer` that would leave it. A failed check, `Rt.exit` and `Sys.exit` end the program without running the deferred statements still pending, so deferred cleanup is for the program's resources, not for what must happen at exit; that is what `Rt.atExit` is for. |