Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Why is creating a temp array with Stride illegal in an Array extension

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!