aboutsummaryrefslogtreecommitdiff
path: root/exercises/090_async7.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-06-14 15:39:49 -0400
committerDave Gauer <dave@ratfactor.com>2021-06-14 15:39:49 -0400
commit8dd9c1ddcb55b5a28f0dbec844a971daceef062e (patch)
tree28f3ba541cb2ca4606bfb8451ac85473a084f2d7 /exercises/090_async7.zig
parent52c424a247e89c38c1e877daf385c4577647e883 (diff)
downloadziglings-8dd9c1ddcb55b5a28f0dbec844a971daceef062e.tar.gz
ziglings-8dd9c1ddcb55b5a28f0dbec844a971daceef062e.tar.bz2
ziglings-8dd9c1ddcb55b5a28f0dbec844a971daceef062e.tar.xz
ziglings-8dd9c1ddcb55b5a28f0dbec844a971daceef062e.zip
add ex090 async 7
Diffstat (limited to 'exercises/090_async7.zig')
-rw-r--r--exercises/090_async7.zig43
1 files changed, 43 insertions, 0 deletions
diff --git a/exercises/090_async7.zig b/exercises/090_async7.zig
new file mode 100644
index 0000000..89113bd
--- /dev/null
+++ b/exercises/090_async7.zig
@@ -0,0 +1,43 @@
+//
+// Remember how a function with 'suspend' is async and calling an
+// async function without the 'async' keyword makes the CALLING
+// function async?
+//
+// fn fooThatMightSuspend(maybe: bool) void {
+// if (maybe) suspend {}
+// }
+//
+// fn bar() void {
+// fooThatMightSuspend(true); // Now bar() is async!
+// }
+//
+// But if you KNOW the function won't suspend, you can make a
+// promise to the compiler with the 'nosuspend' keyword:
+//
+// fn bar() void {
+// nosuspend fooThatMightSuspend(false);
+// }
+//
+// If the function does suspend and YOUR PROMISE TO THE COMPILER
+// IS BROKEN, the program will panic at runtime, which is
+// probably better than you deserve, you oathbreaker! >:-(
+//
+const print = @import("std").debug.print;
+
+pub fn main() void {
+
+ // The main() function can not be async. But we know
+ // getBeef() will not suspend with this particular
+ // invocation. Please make this okay:
+ var my_beef = getBeef(0);
+
+ print("beef? {X}!\n", .{my_beef});
+}
+
+fn getBeef(input: u32) u32 {
+ if (input > 0xDEAD) {
+ suspend {}
+ }
+
+ return 0xBEEF;
+}