| ... | ... | @@ -4628,6 +4628,7 @@ test "while null capture" { |
| 4628 | 4628 | } |
| 4629 | 4629 | try expect(sum1 == 3); |
| 4630 | 4630 | |
| 4631 | // null capture with an else block |
| 4631 | 4632 | var sum2: u32 = 0; |
| 4632 | 4633 | numbers_left = 3; |
| 4633 | 4634 | while (eventuallyNullSequence()) |value| { |
| ... | ... | @@ -4636,6 +4637,7 @@ test "while null capture" { |
| 4636 | 4637 | try expect(sum2 == 3); |
| 4637 | 4638 | } |
| 4638 | 4639 | |
| 4640 | // null capture with a continue expression |
| 4639 | 4641 | var i: u32 = 0; |
| 4640 | 4642 | var sum3: u32 = 0; |
| 4641 | 4643 | numbers_left = 3; |
| ... | ... | @@ -4923,87 +4925,92 @@ test "if boolean" { |
| 4923 | 4925 | } |
| 4924 | 4926 | } |
| 4925 | 4927 | |
| 4926 | | test "if optional" { |
| 4927 | | // If expressions test for null. |
| 4928 | test "if error union" { |
| 4929 | // If expressions test for errors. |
| 4930 | // Note the |err| capture on the else. |
| 4928 | 4931 | |
| 4929 | | const a: ?u32 = 0; |
| 4932 | const a: anyerror!u32 = 0; |
| 4930 | 4933 | if (a) |value| { |
| 4931 | 4934 | try expect(value == 0); |
| 4932 | | } else { |
| 4935 | } else |err| { |
| 4936 | _ = err; |
| 4933 | 4937 | unreachable; |
| 4934 | 4938 | } |
| 4935 | 4939 | |
| 4936 | | const b: ?u32 = null; |
| 4937 | | if (b) |_| { |
| 4940 | const b: anyerror!u32 = error.BadValue; |
| 4941 | if (b) |value| { |
| 4942 | _ = value; |
| 4938 | 4943 | unreachable; |
| 4939 | | } else { |
| 4940 | | try expect(true); |
| 4944 | } else |err| { |
| 4945 | try expect(err == error.BadValue); |
| 4941 | 4946 | } |
| 4942 | 4947 | |
| 4943 | | // The else is not required. |
| 4948 | // The else and |err| capture is strictly required. |
| 4944 | 4949 | if (a) |value| { |
| 4945 | 4950 | try expect(value == 0); |
| 4946 | | } |
| 4951 | } else |_| {} |
| 4947 | 4952 | |
| 4948 | | // To test against null only, use the binary equality operator. |
| 4949 | | if (b == null) { |
| 4950 | | try expect(true); |
| 4953 | // To check only the error value, use an empty block expression. |
| 4954 | if (b) |_| {} else |err| { |
| 4955 | try expect(err == error.BadValue); |
| 4951 | 4956 | } |
| 4952 | 4957 | |
| 4953 | 4958 | // Access the value by reference using a pointer capture. |
| 4954 | | var c: ?u32 = 3; |
| 4959 | var c: anyerror!u32 = 3; |
| 4955 | 4960 | if (c) |*value| { |
| 4956 | | value.* = 2; |
| 4961 | value.* = 9; |
| 4962 | } else |_| { |
| 4963 | unreachable; |
| 4957 | 4964 | } |
| 4958 | 4965 | |
| 4959 | 4966 | if (c) |value| { |
| 4960 | | try expect(value == 2); |
| 4961 | | } else { |
| 4967 | try expect(value == 9); |
| 4968 | } else |_| { |
| 4962 | 4969 | unreachable; |
| 4963 | 4970 | } |
| 4964 | 4971 | } |
| 4972 | {#code_end#} |
| 4973 | {#header_open|if with Optionals#} |
| 4965 | 4974 | |
| 4966 | | test "if error union" { |
| 4967 | | // If expressions test for errors. |
| 4968 | | // Note the |err| capture on the else. |
| 4975 | {#code_begin|test|test_if_optionals#} |
| 4976 | const expect = @import("std").testing.expect; |
| 4969 | 4977 | |
| 4970 | | const a: anyerror!u32 = 0; |
| 4978 | test "if optional" { |
| 4979 | // If expressions test for null. |
| 4980 | |
| 4981 | const a: ?u32 = 0; |
| 4971 | 4982 | if (a) |value| { |
| 4972 | 4983 | try expect(value == 0); |
| 4973 | | } else |err| { |
| 4974 | | _ = err; |
| 4984 | } else { |
| 4975 | 4985 | unreachable; |
| 4976 | 4986 | } |
| 4977 | 4987 | |
| 4978 | | const b: anyerror!u32 = error.BadValue; |
| 4979 | | if (b) |value| { |
| 4980 | | _ = value; |
| 4988 | const b: ?u32 = null; |
| 4989 | if (b) |_| { |
| 4981 | 4990 | unreachable; |
| 4982 | | } else |err| { |
| 4983 | | try expect(err == error.BadValue); |
| 4991 | } else { |
| 4992 | try expect(true); |
| 4984 | 4993 | } |
| 4985 | 4994 | |
| 4986 | | // The else and |err| capture is strictly required. |
| 4995 | // The else is not required. |
| 4987 | 4996 | if (a) |value| { |
| 4988 | 4997 | try expect(value == 0); |
| 4989 | | } else |_| {} |
| 4998 | } |
| 4990 | 4999 | |
| 4991 | | // To check only the error value, use an empty block expression. |
| 4992 | | if (b) |_| {} else |err| { |
| 4993 | | try expect(err == error.BadValue); |
| 5000 | // To test against null only, use the binary equality operator. |
| 5001 | if (b == null) { |
| 5002 | try expect(true); |
| 4994 | 5003 | } |
| 4995 | 5004 | |
| 4996 | 5005 | // Access the value by reference using a pointer capture. |
| 4997 | | var c: anyerror!u32 = 3; |
| 5006 | var c: ?u32 = 3; |
| 4998 | 5007 | if (c) |*value| { |
| 4999 | | value.* = 9; |
| 5000 | | } else |_| { |
| 5001 | | unreachable; |
| 5008 | value.* = 2; |
| 5002 | 5009 | } |
| 5003 | 5010 | |
| 5004 | 5011 | if (c) |value| { |
| 5005 | | try expect(value == 9); |
| 5006 | | } else |_| { |
| 5012 | try expect(value == 2); |
| 5013 | } else { |
| 5007 | 5014 | unreachable; |
| 5008 | 5015 | } |
| 5009 | 5016 | } |
| ... | ... | @@ -5052,6 +5059,7 @@ test "if error union with optional" { |
| 5052 | 5059 | } |
| 5053 | 5060 | } |
| 5054 | 5061 | {#code_end#} |
| 5062 | {#header_close#} |
| 5055 | 5063 | {#see_also|Optionals|Errors#} |
| 5056 | 5064 | {#header_close#} |
| 5057 | 5065 | {#header_open|defer#} |
| ... | ... | @@ -6338,7 +6346,7 @@ test "optional pointers" { |
| 6338 | 6346 | {#code_end#} |
| 6339 | 6347 | {#header_close#} |
| 6340 | 6348 | |
| 6341 | | {#see_also|while with Optionals|if#} |
| 6349 | {#see_also|while with Optionals|if with Optionals#} |
| 6342 | 6350 | {#header_close#} |
| 6343 | 6351 | {#header_open|Casting#} |
| 6344 | 6352 | <p> |