# Inline modifier and reified

**URL:** <https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196>\
**Category:** Language Design\
**Created:** [February 3, 2020, 11:00pm UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196 "2020-02-03T23:00:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![giobalda93](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@giobalda93](https://discuss.kotlinlang.org/u/giobalda93)\
**Post date:** [February 3, 2020, 11:00pm UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196/1 "2020-02-03T23:00:02Z")

</div>

Hi!  
This is my first topic so thanks for having accepted me. Studying Kotlin I wondered why a type reification requires an inlined function. I’ve searched everywhere online but no one has explained this clearly so I am not able to understand. Why the compiler is not able to reify a type parameter without inlining the function first?

Could you help me please?

Thank you in advance  
Giorgio

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 3, 2020, 11:49pm UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196/2 "2020-02-03T23:49:03Z")

</div>

This is a problem of the jvm. What happens is that on the jvm itself does not have generic types (this is called type erasure). So at runtime `List<Foo>` becomes `List` and `List<Bar>` also becomes `List`. Generic information is only available to the compiler, it isn’t available at runtime. This also means that a function call `foo<Int>()` at runtime is just `foo()` so you can no longer use the type parameter inside of the function. You can use it to only for stuff that requires the type parameter at compile time, but not at runtime.

```kotlin
fun <T> foo(arg1: T) {
    val f = listOf<T>(arg1) // ok, T is only used at compile time
    if(f.first is T) // error, this requires T at runtime, so it can't be done
        TODO()
}

```

To reify a type parameter kotlin inlines the type. That way the type is still known at runtime, but this requires the entire function to be inlined. I hope this helps. Let me know if something is still unclear.

---

<div class="post-metadata">

**Author:** ![giobalda93](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@giobalda93](https://discuss.kotlinlang.org/u/giobalda93)\
**Post date:** [February 4, 2020, 8:09am UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196/3 "2020-02-04T08:09:06Z")

</div>

Hi @Wasabi375,  
thanks for your exhaustive response, I am almost there. So inline is needed to save the type before it is erased by JVM, but it cannot be done directly inside the called function.  
Summing up, reified cannot be used without inline because the reification is based on inlining, right?

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [February 4, 2020, 11:38am UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196/4 "2020-02-04T11:38:39Z")

</div>

Yes, inlining is a precondition for reification.

You might have (rightfully) assumed that reification should be possible in some cases without inlining. But without jvm runtime support, how would the compiler do it? The compiler would need to generate code for every invocation of a generic function. This code generation would come at a very big cost if done automatically for every generic function. This cost is made obvious by requiring reified generic functions to be inline.

---

<div class="post-metadata">

**Author:** ![giobalda93](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@giobalda93](https://discuss.kotlinlang.org/u/giobalda93)\
**Post date:** [February 4, 2020, 12:27pm UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196/5 "2020-02-04T12:27:27Z")

</div>

Thank you very much! @fatjoe79  
You two have erased my doubts, better than a JVM.

Have a good day

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 4, 2020, 1:22pm UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196/6 "2020-02-04T13:22:21Z")

</div>

It is probably worth noting that there is another option to reified types that works without inline. Most things you can do with a reified type can also be done using a `Class` instance.

```kotlin
inline fun <reified T>foo(a: Any): Boolean = a is T

fun foo(a: Any, c: Class<*>): Boolean = c.isAssignableFrom(a)
foo("test", String::class.java)

```

This works using reflection, and is how java solves this problem. However reflection can have bad performance and might not work on every system (normal windows/linux/macos/android should be fine though).

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [February 4, 2020, 4:56pm UTC](https://discuss.kotlinlang.org/t/inline-modifier-and-reified/16196/7 "2020-02-04T16:56:34Z")

</div>

Passing a `Class` instance is the traditional Java approach to this sort of problem. But it has disadvantages:

- It loses information on any type parameters that the type itself has (e.g. `List<…>`)
- It loses information on whether the type is nullable.
- It can’t be inferred by the compiler.
- Reflection can be inefficient.
