hn.today

Partial Operator Precedence (Zig Devlog)

ziglang.org4 points0 comments
Screenshot of Partial Operator Precedence (Zig Devlog)

Zig has changed how it handles operator precedence: instead of assigning a fixed order to every operator combination, it leaves some combinations ambiguous and requires parentheses to clarify them. The goal is to reduce the precedence rules programmers must memorize and prevent bugs caused by misremembering how a complex expression is grouped. Familiar combinations, such as ordinary arithmetic and comparisons, can still be written without extra parentheses; unusual mixes such as `orelse` with equality may now produce a compiler error. The change affected only 353 lines in a codebase of more than a million lines. The compiler’s error messages identify the conflicting operators and explain how the expression was grouped in Zig 0.17.x.

Existing code can be updated manually as compiler errors appear, or with the temporary `zig fmt --upgrade` option, which adds parentheses to preserve the old evaluation order. Manual fixes are recommended because reconsidering rare, ambiguous expressions can expose bugs or reveal that a different grouping better reflects the intended logic. Automated formatting can also add unnecessary parentheses, and its preserved grouping may not be the clearest choice - for example, an error handler might more naturally apply to one operand rather than the whole `orelse` expression. Developers who use the formatter are advised to review its changes. The upgrade option will be removed after one release cycle.

Read on ziglang.org0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.