News for hackers. Stuff that matters.
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.