This code throws a compiler error: "Argument type 'StrideTo' expected to be an instance of a class or class-constrained type"
extension Array {
func chunks(_ chunkSize: Int) -> [[Element]] {
let indexes = Array(stride(from: 0, to: count, by: chunkSize)) // This line won't compile
return [[Element]]()
}
}
However, if you use very similar code outside of an Array extension:
let array = Array(stride(from: 0, to: 20, by: 4))
It gives me what I would expect, an array [0, 4, 8, 12, 16].
Why is illegal to create a temporary Array in a function in an Array extension? Is it somehow invoking a stride() instance method on the Array? If so, is there a way to tell the compiler that I want to invoke the global stride() function instead?
That is a bug: SR-13847 Wrong generic used in extensions:
In your case,
Arrayis interpreted asArray<Element>. As a workaround, you can specify the type of the index array explicitly: