Zig NEWS

Discussion on: Cool Zig Patterns - Stable Main Loop

Collapse
 
kristoff profile image
Loris Cro

Nice! This is the same design idea behind std.event.Loop.runDetached: since the async function will not have a surrounding context capable of handling errors, the function you pass in as an argument is required to return void.

github.com/ziglang/zig/blob/master...

/// Runs the provided function asynchronously. The function's frame is allocated
/// with `allocator` and freed when the function returns.
/// `func` must return void and it can be an async function.
/// Yields to the event loop, running the function on the next tick.
pub fn runDetached(self: *Loop, alloc: mem.Allocator, comptime func: anytype, args: anytype) error{OutOfMemory}!void {
   if (!std.io.is_async) @compileError("Can't use runDetached in non-async mode!");
   if (@TypeOf(@call(.{}, func, args)) != void) {
       @compileError("`func` must not have a return value");
    }
   // ...
}
Enter fullscreen mode Exit fullscreen mode