Partial Operator Precedence

(ziglang.org)

24 points | by tosh 3 days ago ago

9 comments

  • skrebbel an 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.

    • pasc1878 8 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 40 minutes 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 an 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 an 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.

  • boxed an 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.

    • xscott a minute ago

      You're not wrong, but

          -b + sqrt(b*b - 4*a*c)/(2*a)
      
          (+ (- b) (/ (sqrt (- (* b b) (* 4 a c)) (* 2 a))))
    • tgv 16 minutes ago

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

    • jraph 31 minutes ago

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