user678392
user678392

Reputation: 2031

Why do I keep getting "Extra Argument in Call" for my initializer in Swift

I keep getting the error "extra argument in call" for repeatedValue in the init function. Why?

 class Point<T> {
/* n dimensional point
 multiline comments ...
*/
let point : [T]
init(dimensions: Int, rValue: Float = 0.0){
    self.point = [T](count: dimensions, repeatedValue:rValue)

}

}

Upvotes: 0

Views: 549

Answers (2)

Alex
Alex

Reputation: 541

If you need the default value your T has also to be a FloatLiteralConvertible, this:

Array<T> init(count: Int, repeatedValue: T)

won't do it. This however will work and makes more sense since you don`t want points made out of for example "Cats" i guess... Solution:

class Point<T:FloatLiteralConvertible> {
    /* n dimensional point
    multiline comments ...
    */
    let point : [T]
    init(dimensions: Int, rValue: T = 0.0 ){
        self.point = [T](count: dimensions, repeatedValue:rValue)

    }
}
var pd = Point<Double>(dimensions: 10, rValue: 1.0)
var pf = Point<Float>(dimensions: 10, rValue: 1.0)
dump(pd.point)
dump(pf.point)

Upvotes: 0

Warren Burton
Warren Burton

Reputation: 17372

The definition for init with repeatedValue is

Array<T> init(count: Int, repeatedValue: T)

Your rValue must be of type T

Upvotes: 3

Related Questions