Kotlin DISCUSSION

How does the reified keyword work in Kotlin inline functions, and what are its limits?

Started by djmad Kotlin reifiedinline functionstype erasuregenericsKClass
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

How does the reified keyword work in Kotlin inline functions, and what are its limits?

djmad Kotlin Forum
#1

I wanted a helper like fun <T> decode(json: String): T that works out the target class from T, but T::class gives the error "Cannot use 'T' as reified type parameter". Changing the declaration to inline fun <reified T> makes it compile.

Why is the type not available in a normal generic function, what does reified change, and are there costs or restrictions I should know about before using it widely?

Community replies 4

Re: How does the reified keyword work in Kotlin inline functions, and what are its limits?

#2

On the JVM, generics are erased. A generic function is compiled once, and that single compiled body serves every T; at runtime there is no record of which type the caller had in mind, so T::class and is T have nothing to work with.

The workaround without reified is to pass the class explicitly: fun <T : Any> decode(json: String, type: KClass<T>): T, called as decode(text, Config::class). It works everywhere, but every caller has to repeat the type.

Re: How does the reified keyword work in Kotlin inline functions, and what are its limits?

#3

reified works because the function is inline. The compiler copies the function body into each call site, and at the call site the concrete type is known, so it substitutes the real class wherever T appears. decode<Config>(text) compiles to code that mentions Config::class directly. Nothing is recovered at runtime; the type is baked in at compile time.

The usual shape is a thin reified wrapper over the explicit version: inline fun <reified T : Any> decode(json: String): T = decode(json, T::class). The standard library's filterIsInstance<T>() is built the same way.

Re: How does the reified keyword work in Kotlin inline functions, and what are its limits?

#4

The restrictions follow from the inlining. It only works on type parameters of inline functions, so not on a class's type parameters and not on members that can be overridden. The type argument at the call site must itself be a concrete type or another reified parameter; you cannot pass along a plain erased T from a non-inline generic function.

A public inline function can only use public API in its body, since that body ends up inside other people's code (@PublishedApi internal is the escape hatch). And Java code cannot call functions with reified type parameters.

Re: How does the reified keyword work in Kotlin inline functions, and what are its limits?

#5

Two practical cautions. The body is duplicated at every call, so keep reified functions small and let them delegate to a normal function that does the real work; a 50-line inline function called from 100 places is 5,000 lines' worth of bytecode.

Also, reification gives you the class, not the full generic type. With T = List<String>, T::class is just List; the element type is still erased there. When the type arguments matter, as they do for JSON decoding of collections, use typeOf<T>(), which returns a KType that includes them. And reified does not let you construct a T; there is no T().

TEP COMMUNITY