# Finding the "type" of a variable

**URL:** <https://discuss.kotlinlang.org/t/finding-the-type-of-a-variable/18317>\
**Category:** Uncategorized\
**Created:** [July 4, 2020, 7:34am UTC](https://discuss.kotlinlang.org/t/finding-the-type-of-a-variable/18317 "2020-07-04T07:34:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hlwerschner](https://avatars.discourse-cdn.com/v4/letter/h/fbc32d/32.png) [@hlwerschner](https://discuss.kotlinlang.org/u/hlwerschner)\
**Post date:** [July 4, 2020, 7:34am UTC](https://discuss.kotlinlang.org/t/finding-the-type-of-a-variable/18317/1 "2020-07-04T07:34:29Z")

</div>

Hi, I seem to have stepped into a smallproblem for which I do not find the solution by reading all tutorials / docs at the jetbrains side:

I have created a very small fun that shall return for a given Type a default value of that same type:

```auto
@JvmStatic
inline fun <reified T> defaultOf() : T { return convertDefaultValues.get(T::class) as T }

```

Its inside of a class and used as a "static class method, for example:

```auto
if (value::class == toType) return value
val defaultValue =
        if (default != Unit) default else defaultOf<String>()

```

This call uses “String” as the T parameter in “defaultOf” and works fine (as far as I currently can test). But in most cases I need a call like

```auto
val defaultValue =
        if (default != Unit) default else defaultOf<value::class ???? >()

```

as I need the default value of the same type as the variable “values” type (including the returned value of defaultOf to be of that same type and NOT Any as this has to be casted to the required type for each call. The idea of defaultOf as a generic function was to delivered exactly the properly typed value as needed at each individual call point.

So my question: How can I specify defaultOf\<value…\>() to use the “type” of value to match that “T” in the fun declaration? My first guess of defaultOfvalue::class() does not work. Is there a way to get to the Type via Kotlin reflection and how to code it?

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [July 4, 2020, 10:24pm UTC](https://discuss.kotlinlang.org/t/finding-the-type-of-a-variable/18317/2 "2020-07-04T22:24:16Z")

</div>

Could you edit your example to use code blocks on the forum? Use triple backticks on a new line (```) to format a code block. Like this “```kotlin fun foo() { }```”  
which looks like this

```auto
fun foo() { }

```

And if you want to make it runnable right here on the forum use “```run-kotlin ```”  
Here’s an example:

```run-kotlin
fun main() { // Press the run button --->
    println("Hello World!")
    // Edit this code right here in the forum!
}

```
