Finding the "type" of a variable

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:

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:

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

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?

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

fun foo() { }

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

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