[swift-evolution] Proposal: change infix operator attributes syntax to be more consistent with the rest of the language

Kevin Lundberg kevin at klundberg.com
Sat Mar 5 22:54:53 CST 2016

Ever since Swift 1.0 I've found the infix operator attribute syntax strange:

infix operator <> { associativity left precedence 100 }

The value of the attribute has no visual cue that ties it to the name of
the attribute. One could arguably read left as its own attribute name
rather than the value set to the associativity attribute, or could read
the wrong pair as one attribute, like "left precedence".

In the spirit of Erica Sadun's recent proposal to make attribute syntax
uniform by using : over =, I'd like to see operator declaration syntax
follow a similar structure:

infix operator <> { associativity: left, precedence: 100 }

In my opinion, this simple change makes operator attributes much more
readable. This also should help future proof readability in operator
attributes if operators gain more of them in the future. A longer list
of attributes with the current syntax would become much harder to read.

The only (minor) sticking point I've noticed is the assignment attribute
in the standard library:
This attribute isn't documented in TSPL and it has no value associated
with it. Since user-defined operators are not meant to be assignment
operators, it may not matter much how these are represented, but
ostensibly some user-defined attribute that has no value may come about
in the future. We could make these attributes simply look like this:

infixoperator+={ associativity:right,precedence: 90,assignment }

or perhaps we could make it so that this attribute must now have a
Boolean value that defaults to false if it isn't provided:

infixoperator+={ associativity:right,precedence: 90,assignment: true }

Any thoughts?

- Kevin

More information about the swift-evolution mailing list