The reason is because you missed the subtle priority of the expression operator.
In your expression:
-4.0.absoluteValue
There are two operators: - (Unary minus operator, prefix) and. (method operator). And that seems to be a priority. overloaded - and the expression was analyzed for:
-(4.0.absoluteValue)
Therefore, you get the result 4.0 absolute value, then turned it over with the unary minus operator and got -4.0.
Swift , . ( ) . . , , .
, . , - , + - */=, .
Btw, . absoluteValue, , .
extension Double {
func absoluteValue() -> Double {
if self < 0 {
return -self
} else {
return self
}
}
}
let myDouble: Double = -7.65
println("The absolute value of \(myDouble) is \(myDouble.absoluteValue()).")