# Deduce type of argument of a Generic class

**URL:** https://discuss.kotlinlang.org/t/deduce-type-of-argument-of-a-generic-class/19938
**Category:** Support
**Created:** [November 17, 2020, 10:56am UTC](https://discuss.kotlinlang.org/t/deduce-type-of-argument-of-a-generic-class/19938 "2020-11-17T10:56:58Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mikhainin](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@mikhainin](https://discuss.kotlinlang.org/u/mikhainin)
#### Post date: [November 17, 2020, 10:56am UTC](https://discuss.kotlinlang.org/t/deduce-type-of-argument-of-a-generic-class/19938/1 "2020-11-17T10:56:58Z")

</div>

Hi there,

Assume, we have classes:

```kotlin
abstract class Descriptor<E : Enum<E>> {
  abstract fun getEnum(): E
}
class First: Descriptor<First.Variant> {
  enum class Variant{ A, B }
  override fun getEnum(): Variant = Variant.A
}
class Second: Descriptor<Second.Variant> {
  enum class Variant{ C, D }
  override fun getEnum(): Variant = Variant.C
}

```

Is there a way not to declare the class parameter if I want to use Descriptor.E,  
now I have to do this:

```kotlin
inline fun <refied T: Descriptor<E>, E: Enum<E>> getVariant() = T().getEnum()
...

// here, I would like NOT to add second parameter, assuming it can be deduced
val enum1 = getVariant<First, First.Variant>()
val enum2 = getVariant<Second, Second.Variant>()

```

I would like to have call looking like:

```kotlin
val enum1 = getVariant<First>()
val enum2 = getVariant<Second>()

```

---

<div class="post-metadata">

### Author: ![akurczak](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@akurczak](https://discuss.kotlinlang.org/u/akurczak)
#### Post date: [November 17, 2020, 4:38pm UTC](https://discuss.kotlinlang.org/t/deduce-type-of-argument-of-a-generic-class/19938/3 "2020-11-17T16:38:38Z")

</div>

I don’t think this is possible. Some connected issues:  
[https://youtrack.jetbrains.com/issue/KT-13394](https://youtrack.jetbrains.com/issue/KT-13394)  
[https://youtrack.jetbrains.com/issue/KT-17061](https://youtrack.jetbrains.com/issue/KT-17061)

So currently it’s only possible to either include all or none type parameters.  
Depending on specific use case you may try the second approach, providing types via parameters and type of variable you assign result to - see example in first comment from second link.

For the specific example, where you instantiate your class within `getVariant` function, you may consider passing `KClass` as parameter instead of using reified type parameters - technically you only write the name of the class and not the name of enum class 😉

```kotlin
fun <T: Descriptor<E>, E : Enum<E>> getVariant(t: KClass<T>) = t.createInstance().getEnum()

```

And call

```kotlin
val enum1 = getVariant(First::class) // First.Variant is inferred
val enum2 = getVariant(Second::class) // Second.Variant is inferred

```
