# Determining the type of a function parameter

**URL:** <https://discuss.kotlinlang.org/t/determining-the-type-of-a-function-parameter/7215>\
**Category:** Language Design\
**Created:** [March 27, 2018, 8:23pm UTC](https://discuss.kotlinlang.org/t/determining-the-type-of-a-function-parameter/7215 "2018-03-27T20:23:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![v79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/v79/32/3297_2.png) [@v79](https://discuss.kotlinlang.org/u/v79)\
**Post date:** [March 27, 2018, 8:23pm UTC](https://discuss.kotlinlang.org/t/determining-the-type-of-a-function-parameter/7215/1 "2018-03-27T20:23:54Z")

</div>

Using reflection, I am examining the constructor parameters of a Kotlin class. I need to determine the class (`KClass`) of each parameter and to take appropriate action:

```
for(consP in primaryConstructor.parameters) {
 when(consP.type) { 
is kotlin.String -> // do String thing;
 is kotlin.Int -> // do Int thing }
 }

```

Unfortunately, the closest I’ve found uses `jvmErasure` and gives me just the qualified name of the parameter:

```
when(consP.type.jvmErasure.qualifiedName) {
  is "kotlin.String" -> // do String thing;
  is "kotlin.Int" -> // do Int thing;
}

```

Is it possible to do what I want without comparing strings? I’m particularly worried about what happens when the class contains Java parameter types (like `java.lang.String`), and don’t want to have to deal with every possible qualified name for Kotlin and Java types.

Edit: I’ve found `consP.type.jvmErasure.java`, which returns the underlying Java class or primitive. But I’m still looking for the KClass ☹

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [March 28, 2018, 6:43pm UTC](https://discuss.kotlinlang.org/t/determining-the-type-of-a-function-parameter/7215/2 "2018-03-28T18:43:38Z")

</div>

Using `consP.type.jvmErasure` as you said you could more or less achieve it:

```
fun main(args: Array<String>) {
    Something::class.primaryConstructor?.parameters?.forEach {
        when (it.type.jvmErasure) {
            String::class -> println("I'm a String")
            Int::class -> println("I'm an Int")
            List::class -> println("I'm a List")
            else -> println("I don't what I am")
        }
    }
}

class Something(string: String, integer: Int, list: List<String>, other: Long)

```

That code outputs:

```nohighlight
I'm a String
I'm an Int
I'm a List
I don't what I am

```

---

<div class="post-metadata">

**Author:** ![v79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/v79/32/3297_2.png) [@v79](https://discuss.kotlinlang.org/u/v79)\
**Post date:** [March 28, 2018, 7:03pm UTC](https://discuss.kotlinlang.org/t/determining-the-type-of-a-function-parameter/7215/3 "2018-03-28T19:03:37Z")

</div>

Oh, thank you! I’ve been struggling for days. I had tried `it.type.jvmErasure` but I thought you had to use the `is` operator when checking the class in a `when` block. So it completely failed.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [March 29, 2018, 8:52am UTC](https://discuss.kotlinlang.org/t/determining-the-type-of-a-function-parameter/7215/4 "2018-03-29T08:52:24Z")

</div>

The is if when you want to use type literals. This code just compares KClass objects - it is reflection based.
