[swift-evolution] [swift-evolution-announce] [Review] SE-0077: Improved operator declarations

Matthew Johnson matthew at anandabits.com
Thu May 19 15:44:50 CDT 2016


> On May 19, 2016, at 3:36 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> I managed to confuse at least two people! I've stated it in the grammar, but forgot to give an example:
>> 
>> ===begin===
>> Multiple precedence relationships can be stated for a single precedence group. Example:
>> ```swift
>> precedencegroup A { }
>> precedencegroup C { }
>> precedencegroup B { precedence(> A) precedence(< C) }
>> ```
>> By transitivity, precedence of C becomes greater than precedence of A.
>> ===end===
>> 
>> As you can see, your suggested syntax would not look good, because there can be any number of precedence declarations.
> 
> Ah, I see.
> 
>> 2. Limit precedence relationships.
>> 
>> Do we really need a full-blown Directed Acyclic Graph?
>> Could `above` and `between` be enough?
>> 
>> Example:
>> 
>> precedencegroup B : between(A, C)
>> 
>> This is one of dark places of the proposal, obviously underdiscussed.
>> Are there practical situations other than `above` and `between`?
>> Do we really need unlimited relationships per one precedencegroup?
> 
> We probably do if you're serious about having operators whose precedence relative to each other is undefined. Moreover, you actually have to be prepared for *more than* two relationships, or two relationships which are both on the same "side", so "between" doesn't cut the mustard.
> 
> I can see three ways to fit multiple relationships on one line:
> 
> 1.	precedence Multiplicative > Additive < BitwiseShift left
> 2.	precedence Multiplicative > Additive, < BitwiseShift left
> 3.	precedence Multiplicative > Additive, Multiplicative < BitwiseShift left
> 
> Another option would be to have `precedence` lines declare-or-redeclare *all* of the precedence levels in them, not just the one on the left of the operator. Then you would write something like:
> 
> 4.	precedence @associativity(left) Multiplicative > Additive
> 	precedence Multiplicative < BitwiseShift
> 
> It would be an error to have two `precedence` lines which marked the same precedence level with a different `@associativity`. Of course, we could instead put associativity on its own line, perhaps allowing multiple declarations for compactness:
> 
> 5.	precedence Multiplicative > Additive
> 	precedence Multiplicative < BitwiseShift
> 	associativity left Cast, Comparative, Multiplicative, Additive
> 
> I think 5 is my preference, but if we want a single-line syntax, I'd probably favor 2.

The problem with 5 is that the declaration can be spread out in code (especially if you really want to declare associativity of several groups together).

The advantage of this approach is that if you wanted / needed to declare precedence relation of two groups imported from different modules you could do that.

How do you feel about the idea of making single line declaration a shorthand, and using braces with lightweight syntax when multiple relations are necessary?  I think that is my preference (unless we decide we need the advantage of your #5 that I described).

> 
> -- 
> Brent Royal-Gordon
> Architechies
> 
> _______________________________________________
> 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