aboutsummaryrefslogtreecommitdiff
path: root/exercises/029_errdefer.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
committerDave Gauer <dave@ratfactor.com>2021-03-12 18:59:46 -0500
commit6ad9774189fbd64b2f2c9519f4513ab34b0c3809 (patch)
treed6c90700131d5b28e898881f13e2a05612e4703f /exercises/029_errdefer.zig
parentbe36352572ddb18218e1830e49316c259dea5e8c (diff)
downloadziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.gz
ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.bz2
ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.xz
ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.zip
"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.
Diffstat (limited to 'exercises/029_errdefer.zig')
-rw-r--r--exercises/029_errdefer.zig59
1 files changed, 59 insertions, 0 deletions
diff --git a/exercises/029_errdefer.zig b/exercises/029_errdefer.zig
new file mode 100644
index 0000000..f43c738
--- /dev/null
+++ b/exercises/029_errdefer.zig
@@ -0,0 +1,59 @@
+//
+// Another common problem is a block of code that could exit in multiple
+// places due to an error - but that needs to run do something before it
+// exits (typically to clean up after itself).
+//
+// An "errdefer" is a defer that only runs if the block exits with an error:
+//
+// {
+// errdefer cleanup();
+// try canFail();
+// }
+//
+// The cleanup() function is called ONLY if the "try" statement returns an
+// error produced by canFail().
+//
+const std = @import("std");
+
+var counter: u32 = 0;
+
+const MyErr = error{ GetFail, IncFail };
+
+pub fn main() void {
+ // We simply quit the entire program if we fail to get a number:
+ var a: u32 = makeNumber() catch return;
+ var b: u32 = makeNumber() catch return;
+
+ std.debug.print("Numbers: {}, {}\n", .{ a, b });
+}
+
+fn makeNumber() MyErr!u32 {
+ std.debug.print("Getting number...", .{});
+
+ // Please make the "failed" message print ONLY if the makeNumber()
+ // function exits with an error:
+ std.debug.print("failed!\n", .{});
+
+ var num = try getNumber(); // <-- This could fail!
+
+ num = try increaseNumber(num); // <-- This could ALSO fail!
+
+ std.debug.print("got {}. ", .{num});
+
+ return num;
+}
+
+fn getNumber() MyErr!u32 {
+ // I _could_ fail...but I don't!
+ return 4;
+}
+
+fn increaseNumber(n: u32) MyErr!u32 {
+ // I fail after the first time you run me!
+ if (counter > 0) return MyErr.IncFail;
+
+ // Sneaky, weird global stuff.
+ counter += 1;
+
+ return n + 1;
+}