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

Brent Royal-Gordon brent at architechies.com
Thu May 19 15:36:31 CDT 2016


> 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.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list