| ... | @@ -4640,6 +4640,7 @@ test "while null capture" { | ... | @@ -4640,6 +4640,7 @@ test "while null capture" { |
| 4640 | } | 4640 | } |
| 4641 | try expect(sum1 == 3); | 4641 | try expect(sum1 == 3); |
| 4642 | | 4642 | |
| | 4643 | // null capture with an else block |
| 4643 | var sum2: u32 = 0; | 4644 | var sum2: u32 = 0; |
| 4644 | numbers_left = 3; | 4645 | numbers_left = 3; |
| 4645 | while (eventuallyNullSequence()) |value| { | 4646 | while (eventuallyNullSequence()) |value| { |
| ... | @@ -4647,6 +4648,15 @@ test "while null capture" { | ... | @@ -4647,6 +4648,15 @@ test "while null capture" { |
| 4647 | } else { | 4648 | } else { |
| 4648 | try expect(sum2 == 3); | 4649 | try expect(sum2 == 3); |
| 4649 | } | 4650 | } |
| | 4651 | |
| | 4652 | // null capture with a continue expression |
| | 4653 | var i: u32 = 0; |
| | 4654 | var sum3: u32 = 0; |
| | 4655 | numbers_left = 3; |
| | 4656 | while (eventuallyNullSequence()) |value| : (i += 1) { |
| | 4657 | sum3 += value; |
| | 4658 | } |
| | 4659 | try expect(i == 3); |
| 4650 | } | 4660 | } |
| 4651 | | 4661 | |
| 4652 | var numbers_left: u32 = undefined; | 4662 | var numbers_left: u32 = undefined; |
| ... | @@ -4927,87 +4937,92 @@ test "if boolean" { | ... | @@ -4927,87 +4937,92 @@ test "if boolean" { |
| 4927 | } | 4937 | } |
| 4928 | } | 4938 | } |
| 4929 | | 4939 | |
| 4930 | test "if optional" { | 4940 | test "if error union" { |
| 4931 | // If expressions test for null. | 4941 | // If expressions test for errors. |
| | 4942 | // Note the |err| capture on the else. |
| 4932 | | 4943 | |
| 4933 | const a: ?u32 = 0; | 4944 | const a: anyerror!u32 = 0; |
| 4934 | if (a) |value| { | 4945 | if (a) |value| { |
| 4935 | try expect(value == 0); | 4946 | try expect(value == 0); |
| 4936 | } else { | 4947 | } else |err| { |
| | 4948 | _ = err; |
| 4937 | unreachable; | 4949 | unreachable; |
| 4938 | } | 4950 | } |
| 4939 | | 4951 | |
| 4940 | const b: ?u32 = null; | 4952 | const b: anyerror!u32 = error.BadValue; |
| 4941 | if (b) |_| { | 4953 | if (b) |value| { |
| | 4954 | _ = value; |
| 4942 | unreachable; | 4955 | unreachable; |
| 4943 | } else { | 4956 | } else |err| { |
| 4944 | try expect(true); | 4957 | try expect(err == error.BadValue); |
| 4945 | } | 4958 | } |
| 4946 | | 4959 | |
| 4947 | // The else is not required. | 4960 | // The else and |err| capture is strictly required. |
| 4948 | if (a) |value| { | 4961 | if (a) |value| { |
| 4949 | try expect(value == 0); | 4962 | try expect(value == 0); |
| 4950 | } | 4963 | } else |_| {} |
| 4951 | | 4964 | |
| 4952 | // To test against null only, use the binary equality operator. | 4965 | // To check only the error value, use an empty block expression. |
| 4953 | if (b == null) { | 4966 | if (b) |_| {} else |err| { |
| 4954 | try expect(true); | 4967 | try expect(err == error.BadValue); |
| 4955 | } | 4968 | } |
| 4956 | | 4969 | |
| 4957 | // Access the value by reference using a pointer capture. | 4970 | // Access the value by reference using a pointer capture. |
| 4958 | var c: ?u32 = 3; | 4971 | var c: anyerror!u32 = 3; |
| 4959 | if (c) |*value| { | 4972 | if (c) |*value| { |
| 4960 | value.* = 2; | 4973 | value.* = 9; |
| | 4974 | } else |_| { |
| | 4975 | unreachable; |
| 4961 | } | 4976 | } |
| 4962 | | 4977 | |
| 4963 | if (c) |value| { | 4978 | if (c) |value| { |
| 4964 | try expect(value == 2); | 4979 | try expect(value == 9); |
| 4965 | } else { | 4980 | } else |_| { |
| 4966 | unreachable; | 4981 | unreachable; |
| 4967 | } | 4982 | } |
| 4968 | } | 4983 | } |
| | 4984 | {#code_end#} |
| | 4985 | {#header_open|if with Optionals#} |
| 4969 | | 4986 | |
| 4970 | test "if error union" { | 4987 | {#code_begin|test|test_if_optionals#} |
| 4971 | // If expressions test for errors. | 4988 | const expect = @import("std").testing.expect; |
| 4972 | // Note the |err| capture on the else. | | |
| 4973 | | 4989 | |
| 4974 | const a: anyerror!u32 = 0; | 4990 | test "if optional" { |
| | 4991 | // If expressions test for null. |
| | 4992 | |
| | 4993 | const a: ?u32 = 0; |
| 4975 | if (a) |value| { | 4994 | if (a) |value| { |
| 4976 | try expect(value == 0); | 4995 | try expect(value == 0); |
| 4977 | } else |err| { | 4996 | } else { |
| 4978 | _ = err; | | |
| 4979 | unreachable; | 4997 | unreachable; |
| 4980 | } | 4998 | } |
| 4981 | | 4999 | |
| 4982 | const b: anyerror!u32 = error.BadValue; | 5000 | const b: ?u32 = null; |
| 4983 | if (b) |value| { | 5001 | if (b) |_| { |
| 4984 | _ = value; | | |
| 4985 | unreachable; | 5002 | unreachable; |
| 4986 | } else |err| { | 5003 | } else { |
| 4987 | try expect(err == error.BadValue); | 5004 | try expect(true); |
| 4988 | } | 5005 | } |
| 4989 | | 5006 | |
| 4990 | // The else and |err| capture is strictly required. | 5007 | // The else is not required. |
| 4991 | if (a) |value| { | 5008 | if (a) |value| { |
| 4992 | try expect(value == 0); | 5009 | try expect(value == 0); |
| 4993 | } else |_| {} | 5010 | } |
| 4994 | | 5011 | |
| 4995 | // To check only the error value, use an empty block expression. | 5012 | // To test against null only, use the binary equality operator. |
| 4996 | if (b) |_| {} else |err| { | 5013 | if (b == null) { |
| 4997 | try expect(err == error.BadValue); | 5014 | try expect(true); |
| 4998 | } | 5015 | } |
| 4999 | | 5016 | |
| 5000 | // Access the value by reference using a pointer capture. | 5017 | // Access the value by reference using a pointer capture. |
| 5001 | var c: anyerror!u32 = 3; | 5018 | var c: ?u32 = 3; |
| 5002 | if (c) |*value| { | 5019 | if (c) |*value| { |
| 5003 | value.* = 9; | 5020 | value.* = 2; |
| 5004 | } else |_| { | | |
| 5005 | unreachable; | | |
| 5006 | } | 5021 | } |
| 5007 | | 5022 | |
| 5008 | if (c) |value| { | 5023 | if (c) |value| { |
| 5009 | try expect(value == 9); | 5024 | try expect(value == 2); |
| 5010 | } else |_| { | 5025 | } else { |
| 5011 | unreachable; | 5026 | unreachable; |
| 5012 | } | 5027 | } |
| 5013 | } | 5028 | } |
| ... | @@ -5056,6 +5071,7 @@ test "if error union with optional" { | ... | @@ -5056,6 +5071,7 @@ test "if error union with optional" { |
| 5056 | } | 5071 | } |
| 5057 | } | 5072 | } |
| 5058 | {#code_end#} | 5073 | {#code_end#} |
| | 5074 | {#header_close#} |
| 5059 | {#see_also|Optionals|Errors#} | 5075 | {#see_also|Optionals|Errors#} |
| 5060 | {#header_close#} | 5076 | {#header_close#} |
| 5061 | {#header_open|defer#} | 5077 | {#header_open|defer#} |
| ... | @@ -6379,6 +6395,8 @@ test "optional pointers" { | ... | @@ -6379,6 +6395,8 @@ test "optional pointers" { |
| 6379 | } | 6395 | } |
| 6380 | {#code_end#} | 6396 | {#code_end#} |
| 6381 | {#header_close#} | 6397 | {#header_close#} |
| | 6398 | |
| | 6399 | {#see_also|while with Optionals|if with Optionals#} |
| 6382 | {#header_close#} | 6400 | {#header_close#} |
| 6383 | {#header_open|Casting#} | 6401 | {#header_open|Casting#} |
| 6384 | <p> | 6402 | <p> |