Reputation: 677
I am feeling a bit stupid... I want to get rid of code duplication and so I need to iterate over some functions (a couple of Darwin geometric ones). But when I try
for function in [Darwin.acos, Darwin.cos, Darwin.sin] { print(function(1.0)) }
I get the error "Ambiguous use of 'acos'". I also tried Darwin.acos(Double)
and got "Ambiguous reference to member 'acos'" and Darwin.acos { $0 }
(and similar) without any luck.
Is it possible to do this? Thank you!
Upvotes: 0
Views: 88
Reputation: 539975
In addition to what Sulthan said:
In a future version of Swift these functions are also available as static functions on the floating point types, compare SE-0246 Generic Math(s) Functions:
let functions = [Double.acos, Double.cos, Double.sin]
for function in functions { print(function(1.0)) }
You can try this now with a current Swift developer snapshot from https://swift.org/download/#snapshots.
Upvotes: 2
Reputation: 130152
You have to specify the type somewhere:
let functions: [(Double) -> Double] = [Darwin.acos, Darwin.cos, Darwin.sin]
for function in functions { print(function(1.0)) }
since the functions are overloaded for different floating point types:
public func acos(_: Float) -> Float
public func acos(_: Double) -> Double
public func acos(_: Float80) -> Float80
Upvotes: 2