# Reified casting of Class/KClass?

**URL:** <https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937>\
**Category:** Uncategorized\
**Created:** [August 28, 2016, 1:06pm UTC](https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937 "2016-08-28T13:06:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [August 28, 2016, 1:06pm UTC](https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937/1 "2016-08-28T13:06:23Z")

</div>

Is there a way to prevent the `unchecked cast` warning in the following example.

```
inline fun <reified T: Any> create(gson: Gson): TypeAdapter<T>? {
     return when {
         T::class == AppState::class -> {
             val adapter: TypeAdapter<AppState> = MyAdapter()
         
             /* this is reported as unchecked */
             return adapter as TypeAdapter<T>
         }
         else -> null
     }
}

```

Is there a way to lose the cast at all, because all the type info should be present.

Thanks!

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [August 29, 2016, 10:17am UTC](https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937/2 "2016-08-29T10:17:40Z")

</div>

Not at this time. Comparing two `KClass` variables does not affect smart casts.

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [August 29, 2016, 10:54am UTC](https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937/3 "2016-08-29T10:54:56Z")

</div>

Ok, but when I use a java-class the type-cast works, just the cast back to `T` doesn’t:

```
fun <T> create(cls: Class<T>): TypeAdapter<T>? {

    if (cls == AppState::class.java) {
        /* smart cast works */
        val cls2: Class<AppState> = cls
        
        val adapter: TypeAdapter<AppState> = ...
        
        /* doesn't work */
        return adapter as TypeAdapter<T>

    }
    return null
}

```

Is this some fundamental problem with generics or could type-inference here be implemented?

---

<div class="post-metadata">

**Author:** ![Stanislav.Erokhin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stanislav.erokhin/32/1764_2.png) [@Stanislav.Erokhin](https://discuss.kotlinlang.org/u/Stanislav.Erokhin)\
**Post date:** [August 29, 2016, 11:57am UTC](https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937/4 "2016-08-29T11:57:16Z")

</div>

The problem here is in definition of smart cast. In kotlin smart cast means that for some **instance** we know better type(because of _if_ checks or something like that). So, when we check, that `cls == AppState::class.java` then we know, that instance inside variable `cls` has type `Class<AppState>`.

Actually, after check `cls == AppState::class.java` we know, that generic parameter `T` is `AppState` and, because of this,  
`TypeAdapter<AppState>` is subtype of `TypeAdapter<T>`. But such analysis is complex and it is not just usual smart cast.  
May be we implement it in far future, but not today.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [August 29, 2016, 2:43pm UTC](https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937/5 "2016-08-29T14:43:30Z")

</div>

> [@Stanislav.Erokhin](#):
>
> May be we implement it in far future, but not today.

@fab1an So, it’s better to file as issue to track this request.

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [August 29, 2016, 6:08pm UTC](https://discuss.kotlinlang.org/t/reified-casting-of-class-kclass/1937/6 "2016-08-29T18:08:34Z")

</div>

Reported [https://youtrack.jetbrains.com/issue/KT-13646](https://youtrack.jetbrains.com/issue/KT-13646)
