# Parameterized and type-safe access to KTypeParameter

**URL:** <https://discuss.kotlinlang.org/t/parameterized-and-type-safe-access-to-ktypeparameter/11826>\
**Category:** Language Design\
**Created:** [March 5, 2019, 4:12pm UTC](https://discuss.kotlinlang.org/t/parameterized-and-type-safe-access-to-ktypeparameter/11826 "2019-03-05T16:12:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hypher](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hypher](https://discuss.kotlinlang.org/u/hypher)\
**Post date:** [March 5, 2019, 4:12pm UTC](https://discuss.kotlinlang.org/t/parameterized-and-type-safe-access-to-ktypeparameter/11826/1 "2019-03-05T16:12:28Z")

</div>

I’d like access to the `KTypeParameter`s of an interface or class for meta-programming, similar to the current access to class members:

```
interface ITyped<X: Number, Y>
val xParam: KTypeParameter<Number> = ITyped::X

```

Of course KTypeParameter or something similar would need to be generic in it’s bound.

Ideally, there would be the ability to resolve the value of a type parameter by a class that defines it, like `TypeToken.resolve` in guava:

```
class Concrete : ITyped<Int, String>

ITyped::X.resolveIn(Concerete::class) // => Int

```

I wasn’t able to find any other discussion about this sort of feature, why it’s apparently missing in Kotlin, and what the best way to do this sort of thing given the current functionality. Thanks.

---

<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:** [March 5, 2019, 4:29pm UTC](https://discuss.kotlinlang.org/t/parameterized-and-type-safe-access-to-ktypeparameter/11826/2 "2019-03-05T16:29:27Z")

</div>

You can access the type parameters of a `KClass` using reflection: [typeParameters - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-class/type-parameters.html)

As for the second part, I don’t think it’s possible due to the [jvm type erasure](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html).

---

<div class="post-metadata">

**Author:** ![hypher](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hypher](https://discuss.kotlinlang.org/u/hypher)\
**Post date:** [March 5, 2019, 4:41pm UTC](https://discuss.kotlinlang.org/t/parameterized-and-type-safe-access-to-ktypeparameter/11826/3 "2019-03-05T16:41:41Z")

</div>

I’m aware I can access the type parameters that way, but that’s specifically what I want to avoid. I have to access them by index, and they aren’t type-safe:

```
interface I<T> 
I::class.typeParameters[1] // This compiles. It shouldn't. There is only 1 type parameter.

```

Accessing them in a type-safe way would prevent this sort of error:

```
interface I<T : Number> 
I::typeParams::U // would not compile, because there is no type parameter U
I::typeParams::T // would compile, resulting in something like TypeParameter<I, Number>

```

Furthermore, it is absolutely possible to resolve the actual type of a parameter \_when it is used as a superclass, super-interface, or implemented interface. See Guava’s `TypeToken` documention, and this article: [Advanced Java generics: retreiving generic type arguments](https://www.javacodegeeks.com/2013/12/advanced-java-generics-retreiving-generic-type-arguments.html)

---

<div class="post-metadata">

**Author:** ![hypher](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hypher](https://discuss.kotlinlang.org/u/hypher)\
**Post date:** [March 5, 2019, 4:51pm UTC](https://discuss.kotlinlang.org/t/parameterized-and-type-safe-access-to-ktypeparameter/11826/4 "2019-03-05T16:51:36Z")

</div>

For now, I can use Guava’s `TypeToken`, but’s it not type-safe or elegant:

```
interface ITyped <X : Number, Y> {
    fun xType(): Class<Number> = TypeToken.of(this::class.java)
        .resolveType(Thing::class.java.typeParameters[0]) // Unsafe index
        .rawType as Class<Number> // Unsafe cast

    fun yType(): Class<Number> = TypeToken.of(this::class.java)
        .resolveType(Thing::class.java.typeParameters[1]) // Unsafe index
        .rawType as Class<Any> // Unsafe cast
}

```

Imagine if it could be like this:

```
interface ITyped <X : Number, Y> {
    fun xType() = ITyped::typeParams::X.resolveIn(this) // KClass<Number>
    fun yType() = ITyped::typeParams::Y.resolveIn(this) // KClass<Any>
}

```
