2 ms·
Coroutines in C++ are a good example, definitely. Rust managed to use the borrow checker to avoid dynamic allocation with coroutines. I wonder how Zig handles t
by Kranar 6y ago
Coroutines in C++ are a good example, definitely. Rust managed to use the borrow checker to avoid dynamic allocation with coroutines. I wonder how Zig handles this, and I hope it's not through undefined behavior.
From reading it over, it looks like Zig does use dynamic memory for coroutines but it requires the memory to be passed in at the point of construction. That has some pros and cons to it and is consistent with Zig's overall approach so I can respect that choice.
- AndyKelley 6y agoNot sure what you mean by dynamic memory. Much like a struct can be stored at any memory address - global, heap, stack - a coroutine ("async function") in zig can be stored at any memory address. Just like `pointer.* = Foo{ .a = 12, .b = 34}`, the pointer decides where the struct memory goes, `pointer.* = async foo();` the pointer decides where the async function's stack frame goes.