From 738a9f6cda62f3570e44dc93f8e200afc2cc1b51 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Wed, 3 Feb 2021 19:19:31 -0500 Subject: Inserted ex. 32 unreachable, added quiz4. --- 32_iferror.zig | 49 ------------------------------------------------- 32_unreachable.zig | 38 ++++++++++++++++++++++++++++++++++++++ 33_iferror.zig | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 34_quiz4.zig | 24 ++++++++++++++++++++++++ README.md | 6 +++--- ziglings | 5 +++-- 6 files changed, 117 insertions(+), 54 deletions(-) delete mode 100644 32_iferror.zig create mode 100644 32_unreachable.zig create mode 100644 33_iferror.zig create mode 100644 34_quiz4.zig diff --git a/32_iferror.zig b/32_iferror.zig deleted file mode 100644 index ed92e94..0000000 --- a/32_iferror.zig +++ /dev/null @@ -1,49 +0,0 @@ -// -// Let's revisit the very first error exercise. This time, we're going to -// look at a special error-handling type of the "if" statement. -// -// if (foo) |value| { -// -// // foo was NOT an error; value is the non-error value of foo -// -// } else |err| { -// -// // foo WAS an error; err is the error value of foo -// -// } -// -// We'll take it even further and use a switch statement to handle -// the error types. -// -const MyNumberError = error{ - TooBig, - TooSmall, -}; - -const std = @import("std"); - -pub fn main() void { - var nums = [_]u8{2,3,4,5,6}; - - for (nums) |num| { - std.debug.print("{}", .{num}); - - var n = numberMaybeFail(num); - if (n) |value| { - std.debug.print("=4. ", .{}); - } else |err| switch (err) { - MyNumberError.TooBig => std.debug.print(">4. ", .{}), - // Please add a match for TooSmall here and have it print: "<4. " - } - } - - std.debug.print("\n", .{}); -} - -// This time we'll have numberMaybeFail() return an error union rather -// than a straight error. -fn numberMaybeFail(n: u8) MyNumberError!u8 { - if(n > 4) return MyNumberError.TooBig; - if(n < 4) return MyNumberError.TooSmall; - return n; -} diff --git a/32_unreachable.zig b/32_unreachable.zig new file mode 100644 index 0000000..c81efac --- /dev/null +++ b/32_unreachable.zig @@ -0,0 +1,38 @@ +// +// Zig has an "unreachable" statement. Use it when you want to tell the +// compiler that a branch of code should never be executed and that the +// mere act of reaching it is an error. +// +// if (true) { +// ... +// } else { +// unreachable; +// } +// +// Here we've made a little virtual machine that performs mathematical +// operations on a single numeric value. It looks great but there's one +// little problem: the switch statement doesn't cover every possible +// value of a u8 number! +// +// WE know there are only three operations but Zig doesn't. Use the +// unreachable statement to make the switch complete. Or ELSE. :-) +// +const std = @import("std"); + +pub fn main() void { + const operations = [_]u8{ 1, 1, 1, 3, 2, 2 }; + + var current_value: u32 = 0; + + for (operations) |op| { + switch (op) { + 1 => { current_value += 1; }, + 2 => { current_value -= 1; }, + 3 => { current_value *= current_value; }, + } + + std.debug.print("{} ", .{current_value}); + } + + std.debug.print("\n", .{}); +} diff --git a/33_iferror.zig b/33_iferror.zig new file mode 100644 index 0000000..ed92e94 --- /dev/null +++ b/33_iferror.zig @@ -0,0 +1,49 @@ +// +// Let's revisit the very first error exercise. This time, we're going to +// look at a special error-handling type of the "if" statement. +// +// if (foo) |value| { +// +// // foo was NOT an error; value is the non-error value of foo +// +// } else |err| { +// +// // foo WAS an error; err is the error value of foo +// +// } +// +// We'll take it even further and use a switch statement to handle +// the error types. +// +const MyNumberError = error{ + TooBig, + TooSmall, +}; + +const std = @import("std"); + +pub fn main() void { + var nums = [_]u8{2,3,4,5,6}; + + for (nums) |num| { + std.debug.print("{}", .{num}); + + var n = numberMaybeFail(num); + if (n) |value| { + std.debug.print("=4. ", .{}); + } else |err| switch (err) { + MyNumberError.TooBig => std.debug.print(">4. ", .{}), + // Please add a match for TooSmall here and have it print: "<4. " + } + } + + std.debug.print("\n", .{}); +} + +// This time we'll have numberMaybeFail() return an error union rather +// than a straight error. +fn numberMaybeFail(n: u8) MyNumberError!u8 { + if(n > 4) return MyNumberError.TooBig; + if(n < 4) return MyNumberError.TooSmall; + return n; +} diff --git a/34_quiz4.zig b/34_quiz4.zig new file mode 100644 index 0000000..43734b7 --- /dev/null +++ b/34_quiz4.zig @@ -0,0 +1,24 @@ +// +// Quiz time. See if you can make this program work! +// +// Solve this any way you like, just be sure the output is: +// +// my_num=42 +// +const std = @import("std"); + +const NumError = error{ IllegalNumber }; + +pub fn main() void { + const stdout = std.io.getStdOut().writer(); + + const my_num: u32 = getNumber(); + + try stdout.print("my_num={}\n", .{my_num}); +} + +// Just don't modify this function. It's "perfect" the way it is. :-) +fn getNumber() NumError!u32 { + if( false ) return NumError.IllegalNumber; + return 42; +} diff --git a/README.md b/README.md index 49adc3e..f8b16d5 100644 --- a/README.md +++ b/README.md @@ -66,10 +66,10 @@ Planned exercises: * [x] While * [x] For * [x] Functions -* [x] Errors -* [x] Defer +* [x] Errors (error/try/catch/if-else-err) +* [x] Defer (and errdefer) * [x] Switch -* [ ] Unreachable +* [x] Unreachable * [ ] Pointers * [ ] Pointer sized integers * [ ] Multi pointers diff --git a/ziglings b/ziglings index 8570f28..be2e829 100755 --- a/ziglings +++ b/ziglings @@ -99,8 +99,9 @@ check_it 28_defer2.zig "(Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done." check_it 29_errdefer.zig "Getting number...got 5. Getting number...failed!" check_it 30_switch.zig "ZIG?" check_it 31_switch2.zig "ZIG!" -check_it 32_iferror.zig "2<4. 3<4. 4=4. 5>4. 6>4." "Seriously, what's the deal with fours?" -#check_it 33_quiz4.zig "foo" "Can you make this work?" +check_it 32_unreachable.zig "1 2 3 9 8 7" +check_it 33_iferror.zig "2<4. 3<4. 4=4. 5>4. 6>4." "Seriously, what's the deal with fours?" +check_it 34_quiz4.zig "my_num=42" "Can you make this work?" echo echo " __ __ _ " -- cgit v1.2.3-ZIG