Billy
Billy

Reputation: 751

Swift 3.1 error: cannot invoke 'stride' with an argument list of type

When I try the following code:

extension Int{

   func hello(to end: Int, by step: Int, task: (Int) -> Void ){

        for i in stride(from: 4, to: 8, by: 2) {

            task(i)
         }
     }
}

And I get the error saying:

error: cannot invoke 'stride' with an argument list of type '(from: Int, to: Int, by: Int)' for i in stride(from: 4, to: 8, by: 2)

note: overloads for 'stride' exist with these partially matching parameter lists: (to: Self, by: Self.Stride), (through: Self, by: Self.Stride) for i in stride(from: 4, to: 8, by: 2)

I don't why this type of error happens

Upvotes: 0

Views: 2220

Answers (1)

Sweeper
Sweeper

Reputation: 271810

This is kind of tricky! :)

Int apparently declares its own stride methods (that's why the compiler shows you that partially matching overloads exist), but somehow I can't access them (compiler says that they are marked unavailable). Since you are in an Int extension, calling stride in this context is equivalent to self.stride. And the stride methods that Int has does not have the arguments from:to:by:, so it does not compile.

You want to specifically refer to the stride method that's global. Just specify the module in which the method is defined, i.e. Swift:

extension Int{

   func hello(to end: Int, by step: Int, task: (Int) -> Void ){

        for i in Swift.stride(from: 4, to: 8, by: 2) {

            task(i)
         }
     }
}

Upvotes: 4

Related Questions