authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-14 19:26:28-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-09-14 19:26:28-04:00
log1d041d3a10ec9187b7c4de6b4eb8a90926ea8d6c
tree9f08adb741ae1bd98baf806ac6ab3f099cb304bf
parent0931dda9a95e14b97a84e60aed424fd8bb5e1232
parent80f3c8d2761bb09ccb43d667e6d2c5f73cf408bc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11664 from vincenzopalazzo/macros/docs

docs: add missed docs for some language feature like `defer` and `errdefer`

1 files changed, 28 insertions(+), 1 deletions(-)

doc/langref.html.in+28-1
......@@ -4862,14 +4862,41 @@ fn deferErrorExample(is_error: bool) !void {
48624862 print("encountered an error!\n", .{});
48634863 }
48644864
4865 // inside a defer method the return statement
4866 // is not allowed.
4867 // The following lines produce the following
4868 // error if uncomment
4869 // ```
4870 // defer.zig:73:9: error: cannot return from defer expression
4871 // return error.DeferError;
4872 // ```
4873 //
4874 //defer {
4875 // return error.DeferError;
4876 //}
4877
48654878 if (is_error) {
48664879 return error.DeferError;
48674880 }
48684881}
48694882
4883// The errdefer keyword support also an alternative syntax to capture the
4884// error generated in case of one error.
4885//
4886// This is useful when during the clean up after an error additional
4887// message want to be printed.
4888fn deferErrorCaptureExample() !void {
4889 errdefer |err| {
4890 std.debug.print("the error is {s}\n", .{@errorName(err)});
4891 }
4892
4893 return error.DeferError;
4894}
4895
48704896test "errdefer unwinding" {
48714897 deferErrorExample(false) catch {};
48724898 deferErrorExample(true) catch {};
4899 deferErrorCaptureExample() catch {};
48734900}
48744901 {#code_end#}
48754902 {#see_also|Errors#}
......@@ -11930,7 +11957,7 @@ fn readU32Be() u32 {}
1193011957 <pre>{#syntax#}errdefer{#endsyntax#}</pre>
1193111958 </th>
1193211959 <td>
11933 {#syntax#}errdefer{#endsyntax#} will execute an expression when control flow leaves the current block if the function returns an error.
11960 {#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.
1193411961 <ul>
1193511962 <li>See also {#link|errdefer#}</li>
1193611963 </ul>