precedencegroup in Swift3

In Swift 2 I had defined operators for Set<Int> as follow, for union of sets :


infix operator + { associativity left precedence 140 }
func + (s1: Set<Int>, s2: Set<Int>) -> Set<Int> {
    return s1.union(s2)
}
infix operator - { associativity left precedence 140 }  /
func - (s1: Set<Int>, s2: Set<Int>) -> Set<Int> {
    return s1.subtract(s2)
}



This is no more accepted in Swift 3 which requires to create precedencegroup.


So, I did the following :


precedencegroup MinusPrecedence {   // 23.12.2016
    associativity: left
    higherThan: AssignmentPrecedence        // predefini
}
precedencegroup PlusPrecedence {
    associativity: left
    higherThan: MinusPrecedence
}

infix operator + : PlusPrecedence // { associativity left precedence 140 }
func +(s1: Set<Int>, s2: Set<Int>) -> Set<Int> {
    return s1.union(s2)
}
infix operator - : MinusPrecedence // { associativity left precedence 140 }  // 8.5.2016
func -(s1: Set<Int>, s2: Set<Int>) -> Set<Int> {
    return s1.subtracting(s2)
}


But it seems to have some side effect, because it seems to set precedence for any use of + or -, not only for sets

For instance, when using ternary operator, I get the following message :


Adjacent operators are in unordered precedence groups 'TernaryPrecedence' and 'MinusPrecedence'


Did I write something wrong ? Or must I define how plus and minus precede vs ternary and other operators ?

Accepted Reply

I think the problem here is that you’re declaring custom operators with the same symbols as existing standard operators. Why don’t you just overload the standard

+
and
-
operators? That is, replace all of your code with this.
func +(s1: Set<Int>, s2: Set<Int>) -> Set<Int> { 
    return s1.union(s2) 
} 
func -(s1: Set<Int>, s2: Set<Int>) -> Set<Int> { 
    return s1.subtracting(s2) 
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I think the problem here is that you’re declaring custom operators with the same symbols as existing standard operators. Why don’t you just overload the standard

+
and
-
operators? That is, replace all of your code with this.
func +(s1: Set<Int>, s2: Set<Int>) -> Set<Int> { 
    return s1.union(s2) 
} 
func -(s1: Set<Int>, s2: Set<Int>) -> Set<Int> { 
    return s1.subtracting(s2) 
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

That being said, is there a good reason why the compiler didn't offer more complaint about the OP's implmenetation? Seems like something the compiler should warn about ie: duplicate symbol declaration?

is there a good reason why the compiler didn't offer more complaint about the OP's implmenetation?

I expect it’s because no one has got around to implementing that diagnostic yet. If you want to aid in that process, I recommend you file a bug with a test case that illustrates the issue.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"