From 6ad9774189fbd64b2f2c9519f4513ab34b0c3809 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Fri, 12 Mar 2021 18:59:46 -0500 Subject: "999 is enough for anybody" triple-zero padding (#18) When I hit 999 exercises, I will finally have reached the ultimate state of soteriological release and no more exercises will be needed. The cycle will be complete. All that will be left is perfect quietude, freedom, and highest happiness. --- exercises/33_iferror.zig | 49 ------------------------------------------------ 1 file changed, 49 deletions(-) delete mode 100644 exercises/33_iferror.zig (limited to 'exercises/33_iferror.zig') diff --git a/exercises/33_iferror.zig b/exercises/33_iferror.zig deleted file mode 100644 index 67777a9..0000000 --- a/exercises/33_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; -} -- cgit v1.2.3-ZIG