mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
add ex085 async 2
This commit is contained in:
parent
3b08761532
commit
54c048b0a0
3 changed files with 35 additions and 0 deletions
|
@ -418,6 +418,10 @@ const exercises = [_]Exercise{
|
||||||
.output = "foo() A",
|
.output = "foo() A",
|
||||||
.hint = "Read the facts. Use the facts.",
|
.hint = "Read the facts. Use the facts.",
|
||||||
},
|
},
|
||||||
|
.{
|
||||||
|
.main_file = "085_async2.zig",
|
||||||
|
.output = "Hello async!",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Check the zig version to make sure it can compile the examples properly.
|
/// Check the zig version to make sure it can compile the examples properly.
|
||||||
|
|
29
exercises/085_async2.zig
Normal file
29
exercises/085_async2.zig
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
//
|
||||||
|
// So, 'suspend' returns control to the place from which it was
|
||||||
|
// called (the "call site"). How do we control back to the
|
||||||
|
// suspended function?
|
||||||
|
//
|
||||||
|
// For that, we have a new keyword called 'resume' which takes an
|
||||||
|
// async function invocation's frame and returns control to it.
|
||||||
|
//
|
||||||
|
// fn fooThatSuspends() void {
|
||||||
|
// suspend;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// var foo_frame = async fooThatSuspends();
|
||||||
|
// resume foo_frame;
|
||||||
|
//
|
||||||
|
// See if you can make this program print "Hello async!".
|
||||||
|
//
|
||||||
|
const print = @import("std").debug.print;
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
var foo_frame = async foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() void {
|
||||||
|
print("Hello ", .{});
|
||||||
|
suspend;
|
||||||
|
print("async!\n", .{});
|
||||||
|
}
|
||||||
|
|
2
patches/patches/085_async2.patch
Normal file
2
patches/patches/085_async2.patch
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
21a22
|
||||||
|
> resume foo_frame;
|
Loading…
Reference in a new issue