How to declare an exponent / power operator with a new priority group in Swift 3?

There have been changes in Swift 3 for Xcode 8 beta 6, and now I cannot declare my operator for power, as I did before:

infix operator ^^ { } public func ^^ (radix: Double, power: Double) -> Double { return pow((radix), (power)) } 

I read a little about it and there is a new change introduced in Xcode 8 beta 6

From this, I assume that I should declare a priority group and use it for my statement as follows:

 precedencegroup ExponentiativePrecedence {} infix operator ^^: ExponentiativePrecedence public func ^^ (radix: Double, power: Double) -> Double { return pow((radix), (power)) } 

Am I going in the right direction to do this job? What should I put inside {} priority groups?

My ultimate goal is to be able to perform operations with a simple operator in swift, for example:

 10 ^^ -12 10 ^^ -24 
+7
operator-precedence swift swift3 xcode8-beta6
Aug 24 '16 at 6:46
source share
1 answer

Your code is already compiling and running - you do not need to determine the priority relation or associativity if you just use the operator in isolation, for example, in the example you specified:

 10 ^^ -12 10 ^^ -24 

However, if you want to work with other operators, as well as combine several exhibitors, you need to define a priority relation that is higher than MultiplicationPrecedence and the right associativity .

 precedencegroup ExponentiativePrecedence { associativity: right higherThan: MultiplicationPrecedence } 

Therefore, the following expression:

 let x = 2 + 10 * 5 ^^ 2 ^^ 3 

will be rated as:

 let x = 2 + (10 * (5 ^^ (2 ^^ 3))) // ^^ ^^ ^^--- Right associativity // || \--------- ExponentiativePrecedence > MultiplicationPrecedence // \--------------- MultiplicationPrecedence > AdditionPrecedence, // as defined by the standard library 

A complete list of standard library priority groups is available in the evolution project .

+8
Aug 24 '16 at 8:09
source share



All Articles