Operator Declarations

I don't know if it is just me, but precedence group declarations seem very much out of place in their current state in Swift.

precedencegroup precedence group name {
    higherThan: lower group names
    lowerThan: higher group names
    associativity: associativity
    assignment: assignment

This declaration is not very Swifty and sticks out like a sore thumb. It seems to me as if an implementation with a protocol of sorts would be better. Maybe something like:

protocol PrecedenceGroup {
    var higherThan: PrecedenceGroup? { get }
    var lowerThan: PrecedenceGroup? { get }
    var associativity: Associativity { get }
    var assignment: Bool { get }

enum Associativity {
    case right, left, none

extension PrecedenceGroup {
    var higherThan: PrecedenceGroup? {
        return nil
    var lowerThan: PrecedenceGroup? {
        return nil
    var associativity: Associativity {
        return .none
    var assignment: Bool {
        return false

To do something of this sorts though, I am pretty sure the compiler should throw errors at compile-time if certain implementations of precedence groups are invalid (e.g. making a precedence group that is sometimes higher than another precedence group).

  • Should the current syntax for precedence groups stay as is?
  • Should the current update be updated in any ways to make it more swifty?
  • Are there any better alternatives?
  • Why are precedence groups more the less out of line with the rest of Swift declarations?
  • If implemented, what should the compiler do to ensure a valid declaration at compile-time?
  • Any other thoughts on the topic.

We can't actually execute arbitrary code in order to resolve the information in these declarations, and I don't see any reason to contort the syntax to pretend that we can. The current syntax is serving its rather obscure purpose perfectly adequately and really does not need to be drastically rethought.

Terms of Service

Privacy Policy

Cookie Policy