andrewz
andrewz

Reputation: 5220

Swift 3: Convert Array<Double> to Array<Float> extension

extension Array where Element : Double {
    public var asArrayOfFloat: [Float] {
        return self.map { return Float(other:$0) } // compiler error
    }
}

I get a compiler error complaining about Float(other:$0) "Argument labels '(other:)' do not match any available overloads." But, $0 is a Double, and there is a Float.init(other:Double) initializer. What's the problem?

EDIT: Changing to Float($0) creates a different compilation error: "Ambiguous use of 'init'", and has 16 candidates.

EDIT: Float.init(other:Double) originally suggested by compiler, snapshot: enter image description here

Upvotes: 1

Views: 476

Answers (2)

andrewz
andrewz

Reputation: 5220

The issue was with where Element : Double ... This needs to be rewritten as where Element == Double (notice the use of == instead of :) because Double is not a protocol but a type. Now compilation works with Float($0) as suggested.

Upvotes: 3

NRitH
NRitH

Reputation: 13903

Get rid of the other: label. If there is an init override that uses that label (FWIW, I don't see one), then it's not a required label.

Upvotes: 1

Related Questions