Async has not been implemented in the self-hosted compiler yet

I have just started learning zig. When I tried to learn async/await, I got the error below:

carlhung@orangepi5:~/externalDisk/projects/zigProjects/playground$ /home/carlhung/zig-linux-aarch64-0.13.0/zig build run
run
└─ run playground
   └─ install
      └─ install playground
         └─ zig build-exe playground Debug native 1 errors
src/main.zig:24:12: error: async has not been implemented in the self-hosted compiler yet
    frame: anyframe,
           ^~~~~~~~
error: the following command failed with 1 compilation errors:
/home/carlhung/zig-linux-aarch64-0.13.0/zig build-exe -ODebug -Mroot=/home/carlhung/externalDisk/projects/zigProjects/playground/src/main.zig --cache-dir /home/carlhung/externalDisk/projects/zigProjects/playground/.zig-cache --global-cache-dir /home/carlhung/.cache/zig --name playground --listen=- 
Build Summary: 2/7 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run playground transitive failure
   ├─ zig build-exe playground Debug native 1 errors
   └─ install transitive failure
      └─ install playground transitive failure
         └─ zig build-exe playground Debug native (reused)
error: the following build command failed with exit code 1:
/home/carlhung/externalDisk/projects/zigProjects/playground/.zig-cache/o/7d48377dd31240794169e97adf718a28/build /home/carlhung/zig-linux-aarch64-0.13.0/zig /home/carlhung/externalDisk/projects/zigProjects/playground /home/carlhung/externalDisk/projects/zigProjects/playground/.zig-cache /home/carlhung/.cache/zig --seed 0x1369ce92 -Z2a9ce52a4416686f run

I checked the version of zig:

$ zig version
0.13.0

my device:

$ cat /etc/os-release
PRETTY_NAME="Armbian 24.11.3 bookworm"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.armbian.com"
SUPPORT_URL="https://forum.armbian.com"
BUG_REPORT_URL="https://www.armbian.com/bugs"
ARMBIAN_PRETTY_NAME="Armbian 24.11.3 bookworm"

I think this FAQ entry might be relevant :slight_smile:

2 Likes

This is disappointing. I think I will come back again once they have implemented async/await.

async/await

To be pedantic, I would reconsider if/when Zig gets native concurrency support, which may or not use the async/await paradigm. In my perfect world, I would personally rather see something more akin to Go’s channels, as I am not a fan of function coloring.

3 Likes

If async comes back it will be colorblind as it was before What is Zig's “Colorblind” Async/Await? | Loris Cro's Blog so function coloring is not an issue

1 Like

Interesting read. I liked his closing statement:

I think goroutines are preferable in that context because async/await is a much lower level tool that is easy to misuse, but when it comes to writing code with critical requirements of correctness and efficiency, you need async/await and you need Zig’s philosophy that we all should strive to write robust , optimal , and reusable software.

This is a very compelling point that I didn’t think of. So long as function-coloring is a non-issue, I wouldn’t mind async/await, it is an intuitive pattern to use.

async/await also has a ‘hidden complexity’ (or even hidden-control-flow) problem, e.g. the function bodies produced by the compiler have not much in common with the sequential code you’re writing since the sequential code is split into a switch-case state machine with an imlicit context parameter.

The only advantage over ‘green-thread coroutines’ is that it works even on runtime environments which don’t allow to switch the stack (but the only relevant target platform which doesn’t allow stack switching is WASM - but that is being worked on: GitHub - WebAssembly/stack-switching: A repository for the stack switching proposal.)

Debuggability is also a concern (I think both approaches need special debugger support).

IMHO: I would like to see that most Zig libraries simply using completion callbacks for asynchronous code as the least common denominator, since completion callbacks are the common base that can then wrapped in async/await, green-threading or multithreading code (e.g. such a Zig library would be usable with all those concurrency and parallelism concepts)

1 Like

Haven’t personally used it but maybe it’s worth looking into using GitHub - libuv/libuv: Cross-platform asynchronous I/O

I don’t personally like callback based async, so I wrote https://github.com/Cloudef/zig-aio

4 Likes