3 Replies
      Latest reply: Dec 26, 2016 1:51 PM by eskimo RSS
      Claude31 Level 5 Level 5 (1,050 points)

        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 ?

        • Re: precedencegroup in Swift3
          eskimo Apple Staff Apple Staff (6,665 points)

          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"

            • Re: precedencegroup in Swift3
              karim Level 1 Level 1 (0 points)

              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?

                • Re: precedencegroup in Swift3
                  eskimo Apple Staff Apple Staff (6,665 points)

                  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"