Partial Operator Precedence

(ziglang.org)

33 points | by tosh 2 days ago

8 comments

  • Someone 12 minutes 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?

  • ozgrakkurt 10 minutes 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()?

  • skrebbel 1 hour 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.

    • qalmakka 6 minutes 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
    • pasc1878 52 minutes 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.

  • sheept 1 hour 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 1 hour 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
  • raphlinus 1 hour 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.

  • IshKebab 43 minutes 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 1 hour 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 1 hour ago
      Made the right call, yes, but at the right time and with the right parameters above all!
    • tgv 1 hour ago
      So did Forth, but that's no guarantee for readability. APL got the worst of both worlds, IMO.