Development

Zig lead makes 'extremely breaking' change to std.io ahead of Async and Await's return

Published

Zig programming language inventor and lead developer Andrew Kelley has pushed a breaking change to std.io.Reader and std.io.Writer, as part of a series of changes that will lead to the return of Async and Await, keywords for asynchronous programming that were in early builds of the compiler but then removed.

The pull request (PR), which Kelley called Writergate, deprecates “all existing std.io readers and writers” in favour of new non-generic std.io.Reader and std.io.Writer.

These are core functions in the Zig standard library. “These changes are extremely breaking,” said Kelley. “I am sorry for that, but I have carefully examined the situation and acquired confidence that this is the direction that Zig needs to go.”

Zig is pre-release software, the latest release being 0.14.1. That said, it is used in production by some bold organisations, with one prominent example being the bun JavaScript toolkit.

There are further major I/O changes on the way for Zig, including reworking code for tls (transport layer security), http, json (JavaScript object notation), compression and expansion libraries, and complete removal of the deprecated APIs. Kelly said these will follow in individual PRs, gradually removing dependencies on the old streaming APIs.

These changes are also needed for the return of Async and Await, Suspend and Resume to the language. These did exist in early builds of Zig, but were of experimental quality and later removed.

In an online presentation of the Zig roadmap last week, Kelley said “the stuff that I did with Async/Await before, it never felt finished, it never felt like it was good enough. I feel like there is a path towards realizing my vision with this new thing.”

When this series of changes, including I/O as an interface and others, is complete, it will have numerous benefits, Kelley said, including resource leak checking, better testing, and even “bring your own operating system … you can take a package that used to have a dependency on the operating system, and now it only has a dependency on the I/O interface. If you make your own operating system, all you have to do is implement this interface .. I can make a reusable package that doesn’t decide which operating system it has to run on, and doesn’t decide what order things have to be run in.”

The price of these improvements is that “changing std.io.Reader and Writer makes you fully rewrite any code that touches it, and it’s a huge pain, so I’m sorry, but I’m strongly convicted that this is the future. It’s going to be great. I’m sorry that I didn’t get it first try,” Kelley said.

Initial response from Zig developers is positive though there is anxiety about breaking existing code. “This new I/O interface is a missing piece for complete reusability of all code in ‘std’ and every other library implemented in Zig going forward. What a brilliant idea and execution,” said one comment to the presentation; though another said that “the reality is that [Zig] is already used in big projects, like Bun. Breaking changes don’t seem possible at this point.”

Another said that while it was exciting to find out what is happening with Zig, “it feels like the language is in perpetual early access hell; when is it ever going to be in a stable 1.0 state?”

Kelley said on Hacker News that he is “looking at the example set by hare with interest.” Hare is another programming language currently in preview, and the reference is to a tool called hare-update which will walk through breaking changes in code and automatically suggest solutions.