# How to pass class parameter with Generics to function

**URL:** <https://discuss.kotlinlang.org/t/how-to-pass-class-parameter-with-generics-to-function/1877>\
**Category:** Uncategorized\
**Created:** [July 21, 2016, 9:17am UTC](https://discuss.kotlinlang.org/t/how-to-pass-class-parameter-with-generics-to-function/1877 "2016-07-21T09:17:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobdy](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@nobdy](https://discuss.kotlinlang.org/u/nobdy)\
**Post date:** [July 21, 2016, 9:17am UTC](https://discuss.kotlinlang.org/t/how-to-pass-class-parameter-with-generics-to-function/1877/1 "2016-07-21T09:17:11Z")

</div>

Hi there,

The following code generate a compiler **Error:(6, 41) Kotlin: Type inference failed. Expected type mismatch: inferred type is Class\<CmdEcho\<\*\>\> but Class\<out CmdEcho\> was expected**

What is a proper way to pass `CmdEcho` to function `call`?

```kotlin
package com.cuma.kotlin

class TestG<X> {
    fun test(): CmdEcho<X> {
        val util = CmdUtil<CmdEcho<X>>()
        return util.call(CmdEcho::class.java)
    }
}

class CmdUtil<Y> {
    fun call(x: Class<out Y>): Y {
        return x.newInstance()
    }
}

class CmdEcho<X> {
}

```

Thanks  
Simon

---

<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:** [July 21, 2016, 12:36pm UTC](https://discuss.kotlinlang.org/t/how-to-pass-class-parameter-with-generics-to-function/1877/2 "2016-07-21T12:36:04Z")

</div>

The code does not compile because `CmdEcho::class.java` does not specify any type parameter for `CmdEcho`; its type is `CmdEcho<*>` which does not match `CmdEcho<X>`. Here’s one possible way to fix this code:

```auto
class TestG<X> {
    fun test(): CmdEcho<X> {
        val util = CmdUtil<CmdEcho<*>>()
        return util.call(CmdEcho::class.java) as CmdEcho<X>
    }
}

class CmdUtil<Y> {
    fun call(x: Class<out Y>): Y {
        return x.newInstance()
    }
}

class CmdEcho<X> {
}

```
