Skip to content

Instantly share code, notes, and snippets.

@saylerb
Last active December 14, 2019 16:54
Show Gist options
  • Save saylerb/545a690de2c114e4265249169630bfb4 to your computer and use it in GitHub Desktop.
Save saylerb/545a690de2c114e4265249169630bfb4 to your computer and use it in GitHub Desktop.
Kotlin reified-types

Reified-types

Normally, you can't pass a generic type to a function and access it as if it were a class. This is because of type erasure: generic types are erased at compile time and aren't normally accessible at runtime.

fun <T> deserializeObject(json: String): T {
      val mapper = jacksonObjectMapper()
                                    
      return mapper.readValue(json, T::class.java) //does not compile!
}

To get around this, we can pass the class to the function:

fun <T> deserializeObject(json: String, clazz: KClass<T>): T {
      val mapper = jacksonObjectMapper()
                                    
      return mapper.readValue(json, clazz.java)
}
...
data class Example(val name: String)
val json = """{"name":"example"}"""

deserializeObject(json, Example::class)

With Kotlin, we can do better! With a reified type, we can tell Kotlin that we want to maintain access to the type at runtime. Functions that use reified types must be inline: the compiler will inline the function's bytecode everywhere the function is called, with the types specified.

inline fun <reified T> deserializeObject(json: String): T {
      val mapper = jacksonObjectMapper()
                                    
      return mapper.readValue(json, T::class.java)
}
...
data class Example(val name: String)
val json = """{"name":"example"}"""

deserializeObject<Example>(json)

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment