Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda