Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

373
Visualizações
How to extend protocol Optional, where Wrapped item is Array of Equatable generic elements?

I would say this problem is about proper declaration of extension.

I would like to extend Array filled with generic Elements, where Element conforms to Equatable. I've managed to do that by :

    extension Array where Element: Equatable{
        // my code
    }

However I'd like to know how to properly declare extension when the Array filled with Equatable elements is inside an Optional? I know that in this case I am actually extending protocol Optional, but I can't figure out the rest

I was thinking something like:

    extension Optional where Wrapped: Array  & Equatable {
      // my code
    }

Can't figure it out. Any ideas ?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I don't think you can't specify this constraint at the extension level, however you should be able to specify it at the function level:

extension Optional {
    func myFunc<T: Equatable>() where Wrapped == [T] {
        // do your thing
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

Cristik provided a good solution. An alternative is to write an extension to an optional collection of equatable elements:

extension Optional where Wrapped: Collection, Wrapped.Element: Equatable {
    func foo() { }
}

This would be applicable to arrays, array slices, and other collections.

Depending on what the extension does, you may want to use a combination of Collection, MutableCollection, BidirectionalCollection, RandomAccessCollection.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda