Partial Operator Precedence (ziglang.org)

59 points by tosh 3 days ago

ozgrakkurt 2 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 2 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 2 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 an hour ago

vidarh an hour ago

flohofwoe an hour ago

bennettnate5 an hour ago

anonymous908213 2 hours ago

shiandow an hour 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.

solatic 2 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.

flohofwoe an hour ago

...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)));
(I bet I messed up the parentheses somewhere).

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 ;)

Luke2642 an hour ago

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

Someone 2 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?

dan-robertson an hour ago

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.

v9v 2 hours ago

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

skrebbel 4 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 9 minutes ago

Things become error prone once you realize that the precedence rules are different across languages!

pasc1878 3 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.

qalmakka 2 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

raphlinus 4 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)?

I'm thinking of doing this for symbolasm also.

elegios 2 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 3 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.

Scaevolus 4 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

IshKebab 3 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?).

boxed 4 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 3 hours ago

Made the right call, yes, but at the right time and with the right parameters above all!

tgv 3 hours ago

So did Forth, but that's no guarantee for readability. APL got the worst of both worlds, IMO.