Partial Operator Precedence

tosh 106 points 70 comments November 02, 2026
ziglang.org · View on Hacker News

Discussion Highlights (16 comments)

boxed

I like the compromise, although I still think operator precedence in itself is evil :P Lisp kinda made the right call by mistake.

Scaevolus

C/C++ obviously have far too many precedence levels, but you can easily have a more readable tower without forcing parentheses, like Go's 5 levels: https://go.dev/ref/spec#Operator_precedence

skrebbel

I never realized that languages have so many strict precedence levels because that’s easier to implement than not having them. This here seems like a very good idea and I hope other languages in active development take note. And als JS’ Prettier, which actively removes parentheses like these.

raphlinus

Worth noting, WGSL does this too, and is mostly a bit stricter than Zig: `a + b & c` and `a << b & c` are errors in WGSL but accepted by Zig. (Quick: off the top of your head, how do those parse)? I'm thinking of doing this for symbolasm also.

sheept

JavaScript does something similar for the exponentiation operator. This is a syntax error: -a ** b Someone who learned PEMDAS in school may argue it should be equivalent to -(a * b), while a programmer may be used to unary operators binding tightest, so (-a) * b.

IshKebab

This is very clearly the best design for operator precedence IMO and it's nice that Zig has converged on it. It would be good to get it as a linting/formatting rule in Rust too (maybe that already exists?).

Someone

I don’t find the article clear on how it fixes things, but I guess that, now a op1 b op2 c is an error if op1 is unequal to op2 and they have the same precedence. Do I see that right? Also, it seems each operator will have to have a associativity to handle a future operator for exponentiation or (also potential future) custom operators. Finally, the “What you need to know” section doesn’t mention orelse . Where does that fit in?

ozgrakkurt

Here is a crazy idea, don’t use operators, just use functions. Operators cause issues because it is not possible to cleanly express error output from an operator, it is not possible to express the bit-width of the operation, have to understand precedence. All of these issues cause other downstream issues. Why not add_u64()?

elegios

This is one of those ideas that is very weird if you’re approaching it from more or less traditional parsing techniques, but makes a lot of sense when you think about it. I ended up doing a bunch of work on it in a slightly broader and more formal setting during my phd ( https://dl.acm.org/doi/abs/10.1145/3571251 ) with the expectation that it probably wouldn’t be relevant for more mainstream languages any time soon, very happy to be wrong on that.

v9v

Something very similar was in Fortress, see 16.2 here https://www.ccs.neu.edu/home/samth/fortress-spec.pdf

Luke2642

Saturating vs wrapping operators are so nice! Apparently compiles to 3 or 4 branchless ops on most architectures.

nitnelave

A bit over 10 years ago, I remember implementing partial operator precedence in Bison, for an internship. I think it was merged, but I'm not sure anyone ever used it for anything :) The age of Bison for new parsers was already mostly over

rwoerz

Funny that they named it "orelse" which sounds like a conditional threat to me :)

kibwen

I would like to see languages go even further. Make like Smalltalk did in 1972 and evaluate operators left-to-right with parentheses for grouping.

andrewla

I personally have been increasingly in favor of eliminating operator precedence, even including associativity. It so rarely adds value and is so easy to misunderstand. The SmallTalk and APL models are at least consistent and operator agnostic, but they are unintuitive to read to inexperienced coders. Heavy nesting of complex expressions is also not easy to comprehend immediately, but it's better than trying to unwind the precedence. That said, maybe this approach is best; just take the small set of operators where we have all internalized the precedence and require it everywhere else.

mncharity

At the moment, there's a "anti-operators" theme in comments. So, in defense of operators... comments> Hard to understand; just use functions So turn our back on mathematics as a language? Instead of a `_ [ _ ]` post-circumfix operator, simple concise understandable, we'll use... `(vector-ref)`? `(aref)`? A hardwired expression grammar rule? comments> Just use scheme/lisp Non-toy scheme parsing is not simple. In implementations where the reader isn't a misbegotten handcrafted hairball, it can be a big set of regexes. Regexes notably not expressed in a verbose functional sexp syntax. So yes, precedence should be a partial order, not a total order. You want comparability of two operators' precedences to be a design choice, not a we-used-ints implementation artifact. Various composition constructs should require disambiguation. But operator parsing is a nice way of obtaining that. And yes, in many language designs, you don't optimize for expression concision, or extensibility, or mathematics as an expressive language. But when you do... yay operator parsing.

Semantic search powered by Rivestack pgvector
3,874 stories · 35,194 chunks indexed