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

286
Vistas
Reified inline function in Kotlin still leading to compiler error on 'is' check

I have the following function:

inline fun <reified T> create(preference: Preference<T>, title: String = ""): DebugOption{
        val type = when (preference) {
            is Preference<String> -> Type.STRING
            is Preference<Boolean> -> Type.BOOLEAN
            else -> Type.STRING
        }

        return DebugOption(type, preference, displayTitle = StringModel(title))
    }

I was expecting to be able to easily perform this 'is' check, since the generic type is reified, but I am still getting a compiler error:

Cannot check for instance of erased type: Preference<String>
Cannot check for instance of erased type: Preference<Boolean>

So I am confused how am I misusing 'reified' / what am I missing here. Is there a problem with using reified generic types as the type parameter of another class?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The problem is that is checks the runtime type of preference, and the runtime type of preference isn't available until all the generic type information has been erased. Reified types are not magic, after all.

What you can do instead, is check T instead, since as you said, T is indeed reified.

val type = when (T::class) {
    String::class -> Type.STRING
    Boolean::class -> Type.BOOLEAN
    else -> Type.STRING
}

But note that if Preference is covariant (like List) or contravariant, this might not work as you expect in some cases. For example:

// suppose Preference is covariant 
// (i.e. the type parameter is declared <out T>)
val pref: Preference<Any> = Preference<Boolean>()
create(pref, "foo")

In this case, T is inferred to be Any, so type will be assigned Type.STRING. If that is unexpected to you, and you want Type.BOOLEAN instead, you might want to use another way to check a preference's type, rather than reified types, as this cannot be determined at compile time.

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