Rendered at 16:10:58 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
andrewla 2 hours ago [-]
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.
raphlinus 8 hours ago [-]
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)?
Funny that they named it "orelse" which sounds like a conditional threat to me :)
hansvm 1 hours ago [-]
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.
zorobo 3 hours ago [-]
Yes, felt this way when I was learning Ada. Semantics differ.
Also, no zig `andthen` :)
skrebbel 8 hours ago [-]
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.
amenghra 5 hours ago [-]
Things become error prone once you realize that the precedence rules are different across languages!
E.g. 1 & 2 == 0:
Python => True
C => 0 (False)
pasc1878 7 hours ago [-]
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.
foldr 11 minutes ago [-]
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.
qalmakka 7 hours ago [-]
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
tsimionescu 42 minutes ago [-]
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.
nitnelave 4 hours ago [-]
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
Someone 7 hours ago [-]
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?
kmill 36 minutes ago [-]
> 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".
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.
Someone 3 hours ago [-]
I would hope they implemented it with precedence numbers, as any other way is likely to be harder to write and lead to inconsistencies.
It’s harder to write because users will want to use expressions with more than one operator such as
b * b - 4 * a * c
a * b + c * d > e * f and g < h
tsimionescu 39 minutes ago [-]
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
5 hours ago [-]
ozgrakkurt 7 hours ago [-]
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()?
anonymous908213 6 hours ago [-]
Because it's ridiculously verbose. Abbreviating extremely common operations makes code far easier to read and understand than not.
> have to understand precedence
Precedence is not in any way an argument against using operators; you could disallow precedence at all (eg. each expression must have at most one operator, or rather two counting the assignment operator) while still having operators. That said, precedence is also very beneficial to readability when not abused.
> Operators cause issues because it is not possible to cleanly express error output from an operator
Incorrect. An operator can simply desugar to functions, and therefore has the exact same error semantic possibilities. If you take issue with a language not returning errors from operators, that is a language design issue, not something inherent to the existence of operators.
epolanski 6 hours ago [-]
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.
OskarS 6 hours ago [-]
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.
vidarh 6 hours ago [-]
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.
bennettnate5 6 hours ago [-]
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)
Mawr 5 hours ago [-]
> (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)
bennettnate5 3 hours ago [-]
What a lovely pipe operator you have there to chain the functions and contribute to code readability :)
Zambyte 6 hours ago [-]
mult(add(a, b, c, d), e)
:)
mtklein 4 hours ago [-]
At that point,
(* (+ a b c d) e)
epolanski 6 hours ago [-]
Hence I said that operators are more readable for longer formulas.
flohofwoe 6 hours ago [-]
> 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.
anonymous908213 6 hours ago [-]
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.
layer8 4 hours ago [-]
For even cleaner expression, let’s just draw the whole program as a syntax tree.
shiandow 5 hours ago [-]
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.
layer8 4 hours ago [-]
People disagree about that characterization of Lisp.
solatic 6 hours ago [-]
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.
6 hours ago [-]
flohofwoe 6 hours ago [-]
...and while at why not also dump the assignment and indexing operators!
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 ;)
Scaevolus 8 hours ago [-]
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
layer8 3 hours ago [-]
`a + b | c + d` is still confusing and prone to misinterpretation in that scheme.
elegios 6 hours ago [-]
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.
sheept 8 hours ago [-]
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.
marcosdumay 6 minutes ago [-]
The unary minus isn't in PEMDAS. Besides, I have never seen anybody that interprets -2^2 as -4 instead of 4.
Luke2642 5 hours ago [-]
Saturating vs wrapping operators are so nice! Apparently compiles to 3 or 4 branchless ops on most architectures.
boxed 8 hours ago [-]
I like the compromise, although I still think operator precedence in itself is evil :P Lisp kinda made the right call by mistake.
jraph 8 hours ago [-]
Made the right call, yes, but at the right time and with the right parameters above all!
7 hours ago [-]
tgv 7 hours ago [-]
So did Forth, but that's no guarantee for readability. APL got the worst of both worlds, IMO.
kibwen 2 hours ago [-]
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.
adrian_b 2 hours ago [-]
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 (i.e. what the C language writes as && and ||).
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.
childintime 35 minutes ago [-]
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.
kibwen 1 hours ago [-]
There's no need to switch evaluation direction, as RPN and concatenative languages like Forth demonstrate. And if one really wants a C-style language with bindings on the LHS, leaving the assignment operator as a special case would still be better than the precedence mess we currently have.
IshKebab 7 hours ago [-]
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?).
kibwen 2 hours ago [-]
> It would be good to get it as a linting/formatting rule in Rust too (maybe that already exists?).
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.
I'm thinking of doing this for symbolasm also.
Also, no zig `andthen` :)
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.
Lisp because everything is explicit and only the form just inside an opening parenthesis does anything. APL as all is scanned right to left.
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?
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...)
Presumably orelse is not in the table because it requires parentheses for any other operator.
It’s harder to write because users will want to use expressions with more than one operator such as
However, the following expressions should be invalid in any language, and no one should write them even in languages where they are unfortunately allowed:
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()?
> have to understand precedence
Precedence is not in any way an argument against using operators; you could disallow precedence at all (eg. each expression must have at most one operator, or rather two counting the assignment operator) while still having operators. That said, precedence is also very beneficial to readability when not abused.
> Operators cause issues because it is not possible to cleanly express error output from an operator
Incorrect. An operator can simply desugar to functions, and therefore has the exact same error semantic possibilities. If you take issue with a language not returning errors from operators, that is a language design issue, not something inherent to the existence of operators.
Also, functions can be used as callbacks, and offer overloads to handle partial applications with ease, pipe(2, sum(3), multiply(2)), e.g.
This Zig change is the best of both worlds: avoid the ambiguity, but keep the readability.
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.
(a + b + c + d) * e
mult(add(a, add(b, add(c, d))), e)
:)
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.
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.
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 ;)
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.
Only in this direction the rule is always consistent, inclusively with assignment and with McCarthy logical operators (i.e. what the C language writes as && and ||).
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.
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: