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

388
Visualizações
Difference between Enum.values() and enumValues() in kotlin

In the official document, I found enumValues() function.

I used enumValues() function, but I cannot find difference.

enum class RGB {
    RED, GREEN, BLUE
}

RGB.values().joinToString { it.name } // RED, GREEN, BLUE
enumValues<RGB>().joinToString { it.name } // RED, GREEN, BLUE

What difference between enumValues() and Enum.values()?

Is it a function for platforms other than JVM? Or are there other use cases?

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

0

The problem with values() is that it only exists on each concrete enum class, and you can't call it on a generic enum to get its values, which is quite useful in some cases. Taking just the simplest example of wanting to access all values in a String, enumValues lets you write a function like this:

inline fun <reified T: Enum<T>> getEnumValuesString(): String {
    // could call RGB.values(), but not T.values()
    // even with the generic constraint and reified generics

    // this works, however
    return enumValues<T>().joinToString()
}

Which can then be called with any enum class you've defined:

getEnumValuesString<RGB>()
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