Ambiguous Use of Operator '-' in Swift with 'Abs()'

Ambiguous use of operator '-' in Swift with 'abs()'

That looks like a compiler bug to me, the type of the literal 3
should be Int. But the compiler complains

error: ambiguous use of operator '-'
var i = -(abs(-3))
^
Swift.-:2:20: note: found this candidate
prefix public func -(x: Float) -> Float
^
Swift.-:2:20: note: found this candidate
prefix public func -(x: Double) -> Double
^
Swift.-:2:20: note: found this candidate
prefix public func -(x: Float80) -> Float80
^
CoreGraphics.-:2:20: note: found this candidate
prefix public func -(x: CGFloat) -> CGFloat

You can solve it with an explicit Int as parameter:

var i = -(abs(-Int(3)))

or with a type annotation on the result:

var i : Int = -(abs(-3))

As @vacawama noticed, there are more possible solutions.
Casting any subexpression to Int makes the compiler happy:

var i1 = -(abs(-(3 as Int)))
var i2 = -(abs((-3) as Int))
var i3 = -(abs(-3) as Int)
var i4 = -(abs(-3)) as Int

Absolute value function abs() unavailable in Swift 3.0. Use abs(_:) free function?

The code you're showing us works for me in Xcode 8 beta 4.

I suppose you have another, different issue causing this misleading error message.

For example, it could be a conflict if you have imported other libraries.

A quick workaround would be to use the complete type:

let myAbsoluteInt = Swift.abs(myInt)

Why can't Swift find a Float extension method when called on an integer literal?

When you don't have an explicit type, Swift assumes either Int or Double. From the Swift book:

For example, if you assign a literal value of 42 to a new constant without saying what type it is, Swift infers that you want the constant to be an Int, because you have initialized it with a number that looks like an integer ... Likewise, if you don’t specify a type for a floating-point literal, Swift infers that you want to create a Double.

Float is not on the inferred type list for literals. If you change your extension to Double, it works (Xcode 7.1):

extension Double {
func printme() {
print("I'm a Double")
}
}

12.printme()
12.0.printme()

How to get the Power of some Integer in Swift language?

If you like, you could declare an infix operator to do it.

// Put this at file level anywhere in your project
infix operator ^^ { associativity left precedence 160 }
func ^^ (radix: Int, power: Int) -> Int {
return Int(pow(Double(radix), Double(power)))
}

// ...
// Then you can do this...
let i = 2 ^^ 3
// ... or
println("2³ = \(2 ^^ 3)") // Prints 2³ = 8

I used two carets so you can still use the XOR operator.

Update for Swift 3

In Swift 3 the "magic number" precedence is replaced by precedencegroups:

precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
infix operator ^^ : PowerPrecedence
func ^^ (radix: Int, power: Int) -> Int {
return Int(pow(Double(radix), Double(power)))
}

// ...
// Then you can do this...
let i2 = 2 ^^ 3
// ... or
print("2³ = \(2 ^^ 3)") // Prints 2³ = 8


Related Topics



Leave a reply



Submit