I want to use **
to overload an exponent function. I works if I use something like "^" but the python way of doing is **
and I would like to use that with Swift. Any way to do that?
error: Operator implementation without matching operator declaration
@infix func ** (num: Double, power: Double) -> Double{
return pow(num, power)
}
println(8.0**3.0) // Does not work
You need to declare the operator before defining the function, as follows:
In Swift 2:
import Darwin
infix operator ** {}
func ** (num: Double, power: Double) -> Double {
return pow(num, power)
}
println(8.0 ** 3.0) // works
In Swift 3:
import Darwin
infix operator **
func ** (num: Double, power: Double) -> Double {
return pow(num, power)
}
print(8.0 ** 3.0) // works