4 ms·
One very cool feature of Zig is that it supports calling async/await from any function [1], not just async functions, which avoids the famous function coloring
by jswny 7y ago
One very cool feature of Zig is that it supports calling async/await from any function [1], not just async functions, which avoids the famous function coloring problem altogether.
[1] https://ziglang.org/#Concurrency-via-Async-Functions https://ziglang.org/#Concurrency-via-Async-Functions
- Arnavion 7y agoI believe function coloring has never been about syntax but about semantics. It's not possible to yield while waiting for an async function to resolve without being async yourself. And if you have a choice between an async and a sync function that you could call from your async function, you should prefer to call the async one to avoid stalling whatever executor is driving your function. Zig isn't avoiding it; it's impossible to avoid. Zig is just inferring the async-ness of the function automatically without you needing to write a keyword for it.
- moonchild 7y ago> function coloring problem If you haven't heard of it: https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-... Highlight: > Wanna know one [programming language] that doesn’t [have this problem]? Java. I know right? How often do you get to say, “Yeah, Java is the one that really does this right.”? But there you go. In their defense, they are actively trying to correct this oversight by moving to futures and async IO. It’s like a race to the bottom.
- qqssccfftt 7y agoI think function colouring is a positive thing. A synchronous function cannot yield; so calling an asynchronous function from it doesn't make sense since those cannot yield either.
- notacoward 7y agoA synchronous function cannot yield, but it can make sure another thread is available to take its place before it issues a blocking syscall. If properly constructed, synchronous stdlib functions would be safe to call from async functions. Any "executor" implementation that uses a fixed-size thread pool and is vulnerable to all threads in that pool being tied up this way at once is BROKEN.