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

Radosław Pietruszewski radexpl at gmail.com
Sun Mar 6 13:42:01 CST 2016


An improvement, but still seems strange to me to use the curly braces with argument-like syntax here. I don’t think there’s another instance of a syntax like this in Swift.

I’d be leaning more towards something like:

infix operator <> (associativity: left, precedence: 100)

or:

operator <> (infix, associativity: left, precedence: 100)

or even:

#operator <> (infix, associativity: left, precedence: 100)

to save up the “operator” keyword

— Radek

> On 06 Mar 2016, at 05:54, Kevin Lundberg via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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:
> https://github.com/apple/swift/blob/12b793c87f160a0f18bb8762e425174205c5d80a/stdlib/public/core/Policy.swift#L427.
> 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
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list