# I have generic type, checkType(10) and compiler say it's Integer

**URL:** <https://discuss.kotlinlang.org/t/i-have-generic-type-checktype-10-and-compiler-say-its-integer/25204>\
**Category:** Uncategorized\
**Created:** [July 14, 2022, 1:37am UTC](https://discuss.kotlinlang.org/t/i-have-generic-type-checktype-10-and-compiler-say-its-integer/25204 "2022-07-14T01:37:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fericandra14](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@fericandra14](https://discuss.kotlinlang.org/u/fericandra14)\
**Post date:** [July 14, 2022, 1:37am UTC](https://discuss.kotlinlang.org/t/i-have-generic-type-checktype-10-and-compiler-say-its-integer/25204/1 "2022-07-14T01:37:16Z")

</div>

\*My expectation  
input: checkType(10)  
output: “Yes! it’s Integer”

This is some mandatory value types.  
• Integer  
• String  
• Boolean  
• Double  
• List  
• Map\<String, String\>

```auto
fun <T> checkType(args: T): String {
   return ""
}

fun main() {
    println(
        """
        '[10, 9, 8 , 6]' is List? ${checkType(listOf(10, 9, 8, 6))}
        'Yeay' is String? ${checkType("Yeay")}
        'True' is Boolean? ${checkType(true)}
        '10.01' is List? ${checkType(10.01)}
    """.trimIndent()
    )
}

```

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 14, 2022, 7:32am UTC](https://discuss.kotlinlang.org/t/i-have-generic-type-checktype-10-and-compiler-say-its-integer/25204/2 "2022-07-14T07:32:32Z")

</div>

I’m not entirely sure what do you need. Something like this?

```kotlin
fun <T> checkType(args: T): String = when (args) {
    is Int -> "Yes! it’s Integer"
    ...
    else -> throw UnsupportedOperationException()
}

```

---

<div class="post-metadata">

**Author:** ![fericandra14](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@fericandra14](https://discuss.kotlinlang.org/u/fericandra14)\
**Post date:** [July 15, 2022, 8:47am UTC](https://discuss.kotlinlang.org/t/i-have-generic-type-checktype-10-and-compiler-say-its-integer/25204/4 "2022-07-15T08:47:44Z")

</div>

My code like this, whether this **best practice**?  
Please review my code

```auto
fun <T> checkType(args: T): String {
    when(args){
        is Int-> return "Yes! it's Integer"
        is String-> return "Yes! it's String"
        is Boolean-> return "Yes! it's boolean"
        is Double-> return "Yes! it's Double"
        is List<*>-> return "Yes! it's List"
        is Map<*, *>-> return "Yes! it's Map"
    }
    return " "
}

```

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [July 15, 2022, 11:40am UTC](https://discuss.kotlinlang.org/t/i-have-generic-type-checktype-10-and-compiler-say-its-integer/25204/5 "2022-07-15T11:40:12Z")

</div>

Maybe something like this would be better:

```run-kotlin
import kotlin.reflect.*
private val acceptedTypes = listOf(Int::class, String::class, Boolean::class, Double::class, List::class, Map::class)
fun <T> checkType(args: T): String {
    return acceptedTypes.firstNotNullOfOrNull { if(it.isInstance(args)) "Yes! it's ${it.simpleName}" else null } ?: " "
}

fun main(){
    println(checkType(listOf(42)))
    println(checkType(42))
    println(checkType("hello world"))
}

```
