News for hackers. Stuff that matters.
Re:Partial Operator Precedence by boxed on Thursday November 05, 2026 @02:54AM (#50376377)
I like the compromise, although I still think operator precedence in itself is evil :P Lisp kinda made the right call by mistake.
Re:Partial Operator Precedence by jraph on Thursday November 05, 2026 @03:32AM (#50376666)
Made the right call, yes, but at the right time and with the right parameters above all!
Re:Partial Operator Precedence by tgv on Thursday November 05, 2026 @03:47AM (#50376792)
So did Forth, but that's no guarantee for readability. APL got the worst of both worlds, IMO.
Re:Partial Operator Precedence by xscott on Thursday November 05, 2026 @04:02AM (#50376900)
You're not wrong, but

    -b + sqrt(b*b - 4*a*c)/(2*a)

    (+ (- b) (/ (sqrt (- (* b b) (* 4 a c)) (* 2 a))))
Re:Partial Operator Precedence by Scaevolus on Thursday November 05, 2026 @02:58AM (#50376407)
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
Re:Partial Operator Precedence by layer8 on Thursday November 05, 2026 @07:42AM (#50378418)
`a + b | c + d` is still confusing and prone to misinterpretation in that scheme.
Re:Partial Operator Precedence by skrebbel on Thursday November 05, 2026 @03:03AM (#50376441)
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.

Re:Partial Operator Precedence by pasc1878 on Thursday November 05, 2026 @03:55AM (#50376843)
No you can have no precedence at all see Lisp and APL for examples.

Lisp because everything is explicit and only the form just inside an opening parenthesis does anything. APL as all is scanned right to left.

Re:Partial Operator Precedence by skrebbel on Friday November 06, 2026 @09:59AM (#50393120)
Obviously. I mean assuming you want to support infix operators at all.
Re:Partial Operator Precedence by fc417fc802 on Friday November 06, 2026 @03:15PM (#50397808)
You had said "that’s easier to implement than not having them" which is what's being objected to there as being plainly incorrect. To your infix comment, for example a strict left to right scan works just fine and is trivial to implement. In that example I guess you'd still strongly prefer to retain precedence for the assignment operator in order to avoid an extra set of parentheses around every expression but that has no effect on the ease of implementing the language itself.
Re:Partial Operator Precedence by qalmakka on Thursday November 05, 2026 @04:40AM (#50377179)
If you use the shunting yard algorithm or similar for infix binary operators it's trivial to define a table with operator precedence values and use that. You can have a bunch of operators all at the same priority but it is the same complexity really, the algorithm is pretty trivial
Re:Partial Operator Precedence by tsimionescu on Thursday November 05, 2026 @10:29AM (#50380328)
The point here is that not all operators should have a concept of precedence. Expressions like "a & b + c >> d" should be invalid, as no human actually understands them unless they spend a lot of careful focus. Instead, such an expression must be written as "((a & b) + c) >> d" explicitly. On the other hand, "a + b + c" and even "a + b * c" are fine, as addition and multiplication (and subtraction and division) are so drilled into everyone in early education.
Re:Partial Operator Precedence by childintime on Thursday November 05, 2026 @12:02PM (#50381523)
> Expressions like "a & b + c >> d" should be invalid

When read procedurally (left to right) it is quite okay though, and no parens are needed (but could be added).

The catch is that `1 + 2 * 3` becomes 9 instead of 7 as math eyes expect. I guess there are things to unlearn and unlearning is always hard. But consider the rules other languages have, like C regarding signed and unsigned, and Javascript regarding comparisons, compared to them this is almost trivial.

Re:Partial Operator Precedence by Joker_vD on Thursday November 05, 2026 @07:34PM (#50386625)
> When read procedurally (left to right) it is quite okay though,

Except that there are almost no circumstances in which "((a & b) + c) >> d" is a sensible expression. The only reasonable interpretation is "(a & b) + (c >> d)", and even then that plus is suspicious.

In practice, the worst offenders are "a & b >> c" and "a & b << c". The first expression is usually intended to mean "(a & b) >> c" — it's a bitfield extraction; the second one is usually intended to mean "a & (b << c)" — it's clearing of all but one bitfield. As you can see, those semantics are impossible to achieve with << and >> having the same precedence.

Re:Partial Operator Precedence by qalmakka on Friday November 06, 2026 @11:56AM (#50394932)
honestly speaking, I think that shifts would almost always be better represented with functions, like lsl(a, 4), which solves the shenanigan between arithmetic/logic shift too.
Re:Partial Operator Precedence by Joker_vD on Friday November 06, 2026 @01:42PM (#50396493)
In that case, I'd rather just have explicit bitfield getter/setter functions à la

    slct(var, 6, 2) = expr;  // var = (var &~ (0b111111 << 2)) | ((expr & 0b111111) << 2);
    var = slct(expr, 6, 2);   // var = (expr >> 2) & 0b111111;
Freaking BCPL had those in 1976, and that language was more primitive than C (it didn't even have any types beyond "machine word, reinterpreted accordingly to the operation applied to it").
Re:Partial Operator Precedence by fc417fc802 on Friday November 06, 2026 @03:31PM (#50398054)
> > When read procedurally (left to right) it is quite okay though,

> Except that there are almost no circumstances in which "((a & b) + c) >> d" is a sensible expression.

That doesn't seem like a convincing argument to me. The question is whether or not the rules set you up for scenarios where misunderstandings are likely. Strict left to right does not - for example, given a & b << c it is immediately clear that the desired interpretation is not the actual one while b << c & a will unmistakeably do what you want.

Re:Partial Operator Precedence by Joker_vD on Saturday November 07, 2026 @02:00AM (#50402309)
> The question is whether or not the rules set you up for scenarios where misunderstandings are likely.

No, this is a misleading statement of the problem because it tacitly assumes that programmers write arbitrary expressions with almost the same probability, so the rules should accommodate them all, at the same expense. This is patently false, and is a horrible policy for any UI/UX/DevEx/etc. designer. The very existence of linters and static analyzers, hell, even the warnings in the modern compilers about suspicious operator precedence and "this expression doesn't mean what you probably think it does" — all that suggests that some kinds expressions are more probable than others; ergo, those should be accommodated for at the expense of the unlikelier ones. So, the precedence levels and PEMDAS it is, instead of the abstractly-pure, from-the-first-principles, strictly left-to-right order which makes only its inventor, with a very peculiar kind of mind, happy at the expense of most of the actual programmers and users of the language.

Re:Partial Operator Precedence by amenghra on Thursday November 05, 2026 @06:34AM (#50377931)
Things become error prone once you realize that the precedence rules are different across languages!
Re:Partial Operator Precedence by nayuki on Thursday November 05, 2026 @11:32AM (#50381173)
Similarly, I don't like people who write code like `if 0`, because 0 is false in C and JavaScript and Python, but 0 is considered true in Ruby!
Re:Partial Operator Precedence by foldr on Thursday November 05, 2026 @10:59AM (#50380785)
Extra precedence levels don't really make parsing any easier. You have to handle precedence level ties anyway (for expressions involving multiple instances of the same operator, like 1 + 2 + 3 + 4 ...). All you have to do is decide whether any given binary operator is right or left associative.
Re:Partial Operator Precedence by raphlinus on Thursday November 05, 2026 @03:08AM (#50376481)
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.

Re:Partial Operator Precedence by sheept on Thursday November 05, 2026 @03:23AM (#50376597)
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.
Re:Partial Operator Precedence by marcosdumay on Thursday November 05, 2026 @11:05AM (#50380865)
The unary minus isn't in PEMDAS. Besides, I have never seen anybody that interprets -2^2 as -4 instead of 4.
Re:Partial Operator Precedence by IshKebab on Thursday November 05, 2026 @04:04AM (#50376909)
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?).
Re:Partial Operator Precedence by kibwen on Thursday November 05, 2026 @08:58AM (#50379124)
> It would be good to get it as a linting/formatting rule in Rust too (maybe that already exists?).

Yes, Clippy has a "precedence" lint that's enabled by default: https://clippy.rust-lang.org/master/index.html?search=preced...

Here's what it looks like:

    warning: operator precedence might not be obvious
     --> src/main.rs:2:5
      |
    2 |     1 << 2 + 3;
      |     ^^^^^^^^^^ help: consider parenthesizing your expression: `1 << (2 + 3)`
      |
      = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.99.0/index.html#precedence
      = note: `#[warn(clippy::precedence)]` on by default
Re:Partial Operator Precedence by Someone on Thursday November 05, 2026 @04:35AM (#50377132)
[delayed]
Re:Partial Operator Precedence by dan-robertson on Thursday November 05, 2026 @05:47AM (#50377622)
The point is that there is no longer a global ‘precedence number’ where the operator with the higher precedence binds tighter. Instead, it is only valid to write your example for some combinations of op1 and op2; other combinations will require explicit parentheses.

Presumably orelse is not in the table because it requires parentheses for any other operator.

Re:Partial Operator Precedence by Someone on Thursday November 05, 2026 @08:15AM (#50378711)
[delayed]
Re:Partial Operator Precedence by tsimionescu on Thursday November 05, 2026 @10:32AM (#50380375)
The first expression is fine, as it uses extremely commonly known precedence rules. The second is more arguable.

However, the following expressions should be invalid in any language, and no one should write them even in languages where they are unfortunately allowed:

  a + b >> c & d && 0 % 9

  a >> 4 ? b : c & d ^ 7
Re:Partial Operator Precedence by fc417fc802 on Friday November 06, 2026 @03:43PM (#50398235)
Given strict left to right parsing I don't see the issue with either of those examples (setting aside that perhaps bool % int ought to trigger a type error). The second example in particular seems perfectly sensible when read this way.

Note that I view ? as a language construct rather than an operator however if for some strange reason someone insisted on including that in the strict left to right requirement the second example still works and is easy to read (I just don't like those semantics as much).

Re:Partial Operator Precedence by tsimionescu on Sunday November 08, 2026 @03:26AM (#50411405)
Contradicting highly ingrained precedence rules for the common operations (+, -, ×) is a huge flaw for strict left to right parsing.

And for ?:, only the part between the ? and the : is implicitly paranthesized. In "a && b ? c : d || e", you need operator precedence to decide if this is "a && (b ? c : d) || e" or "(a && b)? c : (d || e)" or any other combination. So you have to define the precedence of ? and : in relation to other operators in order to define the syntax of your language, so I think it's normal to consider it an operator as well (and I'd say the same for Python's if/else conditional expression, "a if b else c", this is not about the characters used).

Re:Partial Operator Precedence by fc417fc802 on Tuesday November 10, 2026 @02:39PM (#50445229)
Personally I don't see that as a flaw. My concern is how easy a set of rules is to understand versus get wrong at a glance. I think strict left to right is one of the better options owing to how hard it is to inadvertently misread the meaning of an expression.

> for ?:, only the part between the ? and the : is implicitly paranthesized.

That is a choice. If you treat it as an operator, yes. But you don't have to. It can be treated as a language construct. Given the grammar expr ? expr : expr the training portion is akin to a set of brackets or parenthesis, greedily consuming all until end of line.

Re:Partial Operator Precedence by kmill on Thursday November 05, 2026 @10:34AM (#50380421)
> is an error if op1 is unequal to op2 and they have the same precedence. Do I see that right?

It doesn't look like it works that way; only some operator classes are "unchainable".

An older PR description gives more information: https://github.com/ziglang/zig/pull/25275 (There's also a small mention of keyword operators, which I presume `orelse` is. There's a nice diagram showing the precedence classes here: https://codeberg.org/ziglang/zig/pulls/30160#issuecomment-20...)

Re:Partial Operator Precedence by ozgrakkurt on Thursday November 05, 2026 @04:37AM (#50377146)
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()?

Re:Partial Operator Precedence by anonymous908213 on Thursday November 05, 2026 @04:58AM (#50377303)
Because it's ridiculously verbose. Abbreviating extremely common operations makes code far easier to read than not.
Re:Partial Operator Precedence by epolanski on Thursday November 05, 2026 @05:06AM (#50377353)
The amount of times I've been sad that multiply or sum are longer than + or * is limited to long formulas and even then it's a non issue.

Also, functions can be used as callbacks, and offer overloads to handle partial applications with ease, pipe(2, sum(3), multiply(2)), e.g.

Re:Partial Operator Precedence by anonymous908213 on Thursday November 05, 2026 @05:13AM (#50377395)
Operators can also support those things. Operators are just syntax sugar for functions, or at least they're capable of being such. If a language chooses not to support the full range of what functions can do for operators, that is a thing you can criticise the language for (and I do!), but it is not a reason to suggest operators should be disposed with in language design altogether.
Re:Partial Operator Precedence by OskarS on Thursday November 05, 2026 @05:22AM (#50377457)
I don't think your experience is particularly universal, it vastly differs depending on what kind of programming you do. If you're doing games stuff or graphics or signal processing or any other number of subfields of programming that require a lot of math, operators are a god-send for readability.

This Zig change is the best of both worlds: avoid the ambiguity, but keep the readability.

Re:Partial Operator Precedence by bennettnate5 on Thursday November 05, 2026 @05:22AM (#50377458)
I don't think character length of an expression is the issue. Reading chained operators is explicitly easier than reading nested functions by virtue of operators having infix notation and not explicitly requiring parantheses where they are not necessary to disambiguate order of operations. The prefix notation of functions moves the operation farther from the eye from operands and introduces the issue of parantheses-miscounting bugs. I'd hardly say the difference in readability of these is a non issue:

(a + b + c + d) * e

mult(add(a, add(b, add(c, d))), e)

Re:Partial Operator Precedence by epolanski on Thursday November 05, 2026 @05:23AM (#50377464)
Hence I said that operators are more readable for longer formulas.
Re:Partial Operator Precedence by Zambyte on Thursday November 05, 2026 @05:26AM (#50377478)
mult(add(a, b, c, d), e)

:)

Re:Partial Operator Precedence by mtklein on Thursday November 05, 2026 @06:55AM (#50378087)
At that point,

    (* (+ a b c d) e)
Re:Partial Operator Precedence by Mawr on Thursday November 05, 2026 @05:41AM (#50377589)
> (a + b + c + d) * e

   a
   |> add(b)
   |> add(c)
   |> add(d)
   |> mult(e)
> mult(add(a, add(b, add(c, d))), e)

    c
    |> add(d)
    |> add(b, _)
    |> add(a, _)
    |> mult(e)
Re:Partial Operator Precedence by bennettnate5 on Thursday November 05, 2026 @08:38AM (#50378915)
What a lovely pipe operator you have there to chain the functions and contribute to code readability :)
Re:Partial Operator Precedence by xg15 on Thursday November 05, 2026 @08:36PM (#50387060)
There is also some irony that the function style makes the order of the different addition operations explicit - which in this case is not helpful, because the addition operation is associative. So the parentheses here are just noise.

(Well, almost. You might have weird floating point issues or other bugs, where the associativity does not apply anymore and knowing the evaluation order would be useful. But I think this would be rare situations)

Re:Partial Operator Precedence by vidarh on Thursday November 05, 2026 @05:29AM (#50377494)
There are plenty of languages, like e.g. Ruby, where operators are just sugar for functions or methods. Meaning they can still be partially applied, have their value taken, and/or passed as callbacks.

If your language doesn't allow you to do the same thing with operators as you can do with functions, that's a language design issue and has nothing to do with operators as a concept.

Re:Partial Operator Precedence by flohofwoe on Thursday November 05, 2026 @05:40AM (#50377567)
> The amount of times I've been sad that multiply or sum are longer than + or * is limited to long formulas and even then it's a non issue.

It depends on what things you work on. When I'm writing 'math-y' code (GPU shaders for renderng etc) then I want the expressions to look close to the reference paper and not artificially split them up into multiple lines of code with function calls instead of operators.

Re:Partial Operator Precedence by solatic on Thursday November 05, 2026 @05:03AM (#50377337)
Zig sometimes gives you this, for example there are @divCeil, @divTrunc, @divExact, and @divFloor built-ins so that you can be more precise in your intent compared to using "/". It's rather simple to then write a linting rule that fails if you ever have a literal "/" in your codebase.
Re:Partial Operator Precedence by flohofwoe on Thursday November 05, 2026 @05:30AM (#50377504)
...and while at why not also dump the assignment and indexing operators!

    assign(result, add(b, mul(c, sub(d, e)), add(add(f, g), array_get(arr, i));
At that point, even assembly code is more readable. Also there's a reason why Lisp and Forth haven't exactly taken the world by storm ;)
Re:Partial Operator Precedence by shiandow on Thursday November 05, 2026 @05:44AM (#50377609)
Personally I think the meaning of + and * should be context dependent.

Though the main reason for infix notation is to signal associativity. Which a lot of these don't satisfy.

A lot of these are just trying to mimic natural language, which can be useful but as lisp demonstrates is not that important if the grammar is clear enough.

Re:Partial Operator Precedence by layer8 on Thursday November 05, 2026 @07:35AM (#50378364)
People disagree about that characterization of Lisp.
Re:Partial Operator Precedence by layer8 on Thursday November 05, 2026 @07:37AM (#50378386)
For even cleaner expression, let’s just draw the whole program as a syntax tree.
Re:Partial Operator Precedence by elegios on Thursday November 05, 2026 @04:55AM (#50377277)
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.

Re:Partial Operator Precedence by v9v on Thursday November 05, 2026 @05:09AM (#50377379)
[delayed]
Re:Partial Operator Precedence by Luke2642 on Thursday November 05, 2026 @05:54AM (#50377671)
Saturating vs wrapping operators are so nice! Apparently compiles to 3 or 4 branchless ops on most architectures.

* / % | % + - +% -% +| -| == != < > <= >= and or

Re:Partial Operator Precedence by nitnelave on Thursday November 05, 2026 @07:09AM (#50378185)
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
Re:Partial Operator Precedence by rwoerz on Thursday November 05, 2026 @08:04AM (#50378617)
Funny that they named it "orelse" which sounds like a conditional threat to me :)
Re:Partial Operator Precedence by zorobo on Thursday November 05, 2026 @08:21AM (#50378773)
Yes, felt this way when I was learning Ada. Semantics differ.

Also, no zig `andthen` :)

Re:Partial Operator Precedence by hansvm on Thursday November 05, 2026 @10:05AM (#50380004)
It is a threat! You're going to give me the data I asked for orelse I'm going to pull some BS out of thin air.
Re:Partial Operator Precedence by kibwen on Thursday November 05, 2026 @09:00AM (#50379156)
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.
Re:Partial Operator Precedence by adrian_b on Thursday November 05, 2026 @09:06AM (#50379222)
In reality it is much better to evaluate all operators from right to left, as APL does since 1962 (with parentheses when you want another order).

Only in this direction the rule is always consistent, inclusively with assignment and with McCarthy logical operators.

Moreover, only with this evaluation direction the non-commutative operators lead to useful operations when applied to arrays.

In order to make evaluation from left to right always consistent, far more changes would have to be done to the syntax of a programming language, than with the APL evaluation rule.

Re:Partial Operator Precedence by kibwen on Thursday November 05, 2026 @10:03AM (#50379980)
There's no need to switch evaluation direction, as RPN and concatenative languages like Forth demonstrate.
Re:Partial Operator Precedence by adrian_b on Thursday November 05, 2026 @02:24PM (#50383250)
RPN and concatenative languages is what I meant when I said that for a consistent left-to-right evaluation you must make much more significant changes to the syntax of most programming languages (which are ALGOL-like) than with the APL evaluation rule.

You are right that RPN/concatenative languages is the alternative to the APL-like syntax, but they are even more distant from the traditional mathematical notation.

Re:Partial Operator Precedence by kibwen on Tuesday November 10, 2026 @05:44PM (#50447361)
Worth noting that I have no love whatsoever for traditional mathematical notation. We can do better.
Re:Partial Operator Precedence by childintime on Thursday November 05, 2026 @10:35AM (#50380435)
A procedural language is read top-down and left-to-right, so left-to-right is naturally favored. Without an example I don't get how right-to-left avoids the gotchas with non-commutative operators you hint at.
Re:Partial Operator Precedence by adrian_b on Thursday November 05, 2026 @02:37PM (#50383414)
In most programming languages, assignments are evaluated right-to-left, not left-to-right, which is in contradiction with the left-to-right evaluation used for most operators. The same is true for a few other syntactic forms.

The most frequently used non-commutative operators are subtraction and division.

With left-to-right evaluation, an expression like x1 - x2 - x3 - x4 means x1 - (x2 + x3 + x4), which is seldom a useful expression, but with right-to-left evaluation it becomes an alternating sum, i.e. what in left-to-right evaluation would be written as x1 - x2 + x3 - x4.

Given an array of numbers, it is very frequent to want their sum or their alternating sum, which can be obtained by applying "+" or "-" to it.

The same happens with division where dividing the first number by the product of the following is very seldom useful, but an alternating product, i.e. the product of the ratios of consecutive numbers, is frequently needed.

The same happens for other less common operators, the alternating operations are frequently useful.

Re:Partial Operator Precedence by andrewla on Thursday November 05, 2026 @09:19AM (#50379374)
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.

Re:Partial Operator Precedence by unrented7977 on Thursday November 05, 2026 @03:06PM (#50383780)
After being gaslit by C's operator precedence for my entire career, I've learned to just ignore it and add parentheses around every pair of operands until my linter starts crying.
Re:Partial Operator Precedence by Tarq0n on Thursday November 05, 2026 @03:52PM (#50384319)
Nesting can be ameliorated with a pipe operator.
Re:Partial Operator Precedence by mncharity on Thursday November 05, 2026 @06:54PM (#50386287)
[delayed]