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 {...@@ -4862,14 +4862,41 @@ fn deferErrorExample(is_error: bool) !void {
4862 print("encountered an error!\n", .{});4862 print("encountered an error!\n", .{});
4863 }4863 }
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
4865 if (is_error) {4878 if (is_error) {
4866 return error.DeferError;4879 return error.DeferError;
4867 }4880 }
4868}4881}
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
4870test "errdefer unwinding" {4896test "errdefer unwinding" {
4871 deferErrorExample(false) catch {};4897 deferErrorExample(false) catch {};
4872 deferErrorExample(true) catch {};4898 deferErrorExample(true) catch {};
4899 deferErrorCaptureExample() catch {};
4873}4900}
4874 {#code_end#}4901 {#code_end#}
4875 {#see_also|Errors#}4902 {#see_also|Errors#}
...@@ -11930,7 +11957,7 @@ fn readU32Be() u32 {}...@@ -11930,7 +11957,7 @@ fn readU32Be() u32 {}
11930 <pre>{#syntax#}errdefer{#endsyntax#}</pre>11957 <pre>{#syntax#}errdefer{#endsyntax#}</pre>
11931 </th>11958 </th>
11932 <td>11959 <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.
11934 <ul>11961 <ul>
11935 <li>See also {#link|errdefer#}</li>11962 <li>See also {#link|errdefer#}</li>
11936 </ul>11963 </ul>