# Inferring the generic type of a generic?

**URL:** https://discuss.kotlinlang.org/t/inferring-the-generic-type-of-a-generic/7733
**Category:** Support
**Created:** [May 8, 2018, 7:52pm UTC](https://discuss.kotlinlang.org/t/inferring-the-generic-type-of-a-generic/7733 "2018-05-08T19:52:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jackwilsdon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jackwilsdon/32/5102_2.png) [@jackwilsdon](https://discuss.kotlinlang.org/u/jackwilsdon)
#### Post date: [May 8, 2018, 7:52pm UTC](https://discuss.kotlinlang.org/t/inferring-the-generic-type-of-a-generic/7733/1 "2018-05-08T19:52:10Z")

</div>

Is it possible to infer the generic type of a generic in a reified function? For example;

```auto
inline fun <reified T: List<U>> getListType() = U::class.java

```

I know I can do it this way;

```auto
inline fun <reified U, reified T: List<U>> getListType() = U::class.java

```

But I was wondering if it’s possible to extract the generic type of the generic in the first one, so it can just be called like `getListType<ArrayList<String>>` as opposed to `getListType<String, ArrayList<String>()`.

The actual purpose of the function I’m trying to write isn’t to get the type of an element of an array, but the generic type of a deserializer.

---

<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: [May 8, 2018, 8:11pm UTC](https://discuss.kotlinlang.org/t/inferring-the-generic-type-of-a-generic/7733/2 "2018-05-08T20:11:56Z")

</div>

As far as I know it is not possible. The problem is that javas type erasure does delete all type information about generics at runtime. At runtime there is no difference between `List<String>` and `List<Foo>`.

I was thinking that you maybe could use reflection like this

```kotlin
inline fun <reified T: List<*>> getListType() = U::class.typeParameters[0]

```

but this does not seem to give any useful information (The result would be `out E`.)

btw you need to use a wildcard(`*`) instead of `U` if you dont’t have U as another type parameter.

---

<div class="post-metadata">

### Author: ![jackwilsdon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jackwilsdon/32/5102_2.png) [@jackwilsdon](https://discuss.kotlinlang.org/u/jackwilsdon)
#### Post date: [May 8, 2018, 8:16pm UTC](https://discuss.kotlinlang.org/t/inferring-the-generic-type-of-a-generic/7733/3 "2018-05-08T20:16:43Z")

</div>

Ah, that’s a shame. Thanks anyway!
