hn.today

Partial Operator Precedence

ziglang.org4 points0 comments
Screenshot of Partial Operator Precedence

Zig’s master branch now treats selected combinations of operators as ambiguous instead of assigning them a fixed precedence. When an expression mixes such operators, the compiler requires parentheses, making the intended grouping explicit rather than relying on programmers to remember a long and sometimes surprising precedence hierarchy. Familiar combinations, such as addition with multiplication and comparisons, can still be written without extra parentheses where their precedence remains unambiguous. The change aims to reduce mistakes caused by misremembered rules; in a deliberately complex example, adding parentheses makes the grouping visible even though the expression remains hard to read.

The change implements a proposal first opened in 2016, and its practical impact appears limited: only 353 lines in Zig’s million-plus-line codebase needed edits. Existing code can be updated manually as compiler errors arise, or with the temporary `zig fmt --upgrade` option, which inserts parentheses while preserving the old evaluation order. Manual fixes are recommended because reconsidering unusual expressions can reveal bugs; automated changes should also be reviewed for unexpected groupings or redundant parentheses. Going forward, programmers need to learn only the precedence rules that remain unambiguous: the compiler will flag other operator combinations and indicate where clarification is needed.

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.