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

277
Views
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 answers
Answer question

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 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!