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:
For some reason when calling initializer in extension, compiler tries to match unrelated generics.
In your case, Array is interpreted as Array<Element>. As a workaround, you can specify the type of the index array explicitly:
let indexes = Array<Int>(stride(from: 0, to: count, by: chunkSize))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With