News for hackers. Stuff that matters.
Re:Better Operator Precedence by mayoff on Friday October 29, 2021 @11:07PM (#29045660)
Swift works the way this article describes, except that instead of the rules being built in to the compiler, they are defined by the standard library, and you can define your own precedence levels and associativity for custom operators.

https://github.com/apple/swift/blob/3ea9e9e55281b9957d2b5486...

Re:Better Operator Precedence by daniellehmann on Sunday October 31, 2021 @10:27AM (#29057205)
I don't know Swift, but does this mean there are expressions in Swift where the compiler returns an error, because the expression is ambiguous without additional parentheses?

(My understanding of the article is that it mainly argues for _partial_ precedence, not so much that precedence/associativity can be defined in the program - the latter is also the case in other languages, e.g., Haskell.)

Re:Better Operator Precedence by superlopuh on Sunday October 31, 2021 @11:01AM (#29057446)
Yes:

    precedencegroup MyPrecedence {}
    infix operator +++: MyPrecedence
    func +++ (lhs: Int, rhs: Int) -> Int { lhs + rhs }
    let a = 1 + 2 +++ 3
Error:

    Adjacent operators are in unordered precedence groups 'AdditionPrecedence' and 'MyPrecedence'
Re:Better Operator Precedence by daniellehmann on Sunday October 31, 2021 @11:07AM (#29057501)
Very cool, thanks a lot for the example!