swift3 - How to declare exponent/power operator with new precedencegroup in Swift 3? -
there's been change in swift 3 xcode 8 beta 6 , i'm not able declare operator power did before:
infix operator ^^ { } public func ^^ (radix: double, power: double) -> double { return pow((radix), (power)) }
i've read bit , there's new change been introduced in xcode 8 beta 6
from i'm guessing have declare precedence group , use operator this:
precedencegroup exponentiativeprecedence {} infix operator ^^: exponentiativeprecedence public func ^^ (radix: double, power: double) -> double { return pow((radix), (power)) }
am going in right direction make work? should put inside {} of precedence group?
my final aim able make power operations simple operator in swift, e.g.:
10 ^^ -12 10 ^^ -24
your code compiles , runs – don't need define precedence relationship or associativity if you're using operator in isolation, such in example gave:
10 ^^ -12 10 ^^ -24
however, if want work other operators, chaining multiple exponents, you'll want define precedence relationship that's higher than multiplicationprecedence
, a right associativity.
precedencegroup exponentiativeprecedence { associativity: right higherthan: multiplicationprecedence }
therefore following expression:
let x = 2 + 10 * 5 ^^ 2 ^^ 3
will evaluated as:
let x = 2 + (10 * (5 ^^ (2 ^^ 3))) // ^^ ^^ ^^--- right associativity // || \--------- exponentiativeprecedence > multiplicationprecedence // \--------------- multiplicationprecedence > additionprecedence, // defined standard library
the full list of standard library precedence groups available on the evolution proposal.
Comments
Post a Comment