3 ms·
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 c
by mayoff 5y ago
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/3ea9e9e55281b9957d2b5486af62ae83e99a4e6c/stdlib/public/core/Policy.swift#L332 https://github.com/apple/swift/blob/3ea9e9e55281b9957d2b5486...
- deleted 5y ago[deleted]
- daniellehmann 5y agoI 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.)
- superlopuh 5y agoYes: 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'
- daniellehmann 5y agoVery cool, thanks a lot for the example!