Skip to content

Commit

Permalink
Merge pull request #11664 from vincenzopalazzo/macros/docs
Browse files Browse the repository at this point in the history
docs: add missed docs for some language feature like `defer` and `errdefer`
  • Loading branch information
andrewrk committed Sep 14, 2022
2 parents 0931dda + 80f3c8d commit 1d041d3
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -4862,14 +4862,41 @@ fn deferErrorExample(is_error: bool) !void {
print("encountered an error!\n", .{});
}

// inside a defer method the return statement
// is not allowed.
// The following lines produce the following
// error if uncomment
// ```
// defer.zig:73:9: error: cannot return from defer expression
// return error.DeferError;
// ```
//
//defer {
// return error.DeferError;
//}

if (is_error) {
return error.DeferError;
}
}

// The errdefer keyword support also an alternative syntax to capture the
// error generated in case of one error.
//
// This is useful when during the clean up after an error additional
// message want to be printed.
fn deferErrorCaptureExample() !void {
errdefer |err| {
std.debug.print("the error is {s}\n", .{@errorName(err)});
}

return error.DeferError;
}

test "errdefer unwinding" {
deferErrorExample(false) catch {};
deferErrorExample(true) catch {};
deferErrorCaptureExample() catch {};
}
{#code_end#}
{#see_also|Errors#}
Expand Down Expand Up @@ -11930,7 +11957,7 @@ fn readU32Be() u32 {}
<pre>{#syntax#}errdefer{#endsyntax#}</pre>
</th>
<td>
{#syntax#}errdefer{#endsyntax#} will execute an expression when control flow leaves the current block if the function returns an error.
{#syntax#}errdefer{#endsyntax#} will execute an expression when control flow leaves the current block if the function returns an error, the errdefer expression can capture the unwrapped value.
<ul>
<li>See also {#link|errdefer#}</li>
</ul>
Expand Down

0 comments on commit 1d041d3

Please sign in to comment.