Something like this ```fun <T> getAs(clazz: ...
# codereview
p
Something like this
Copy code
fun <T> getAs(clazz: Class<T>): T {
    return getAs {
        when (clazz) {
            Int::class.java -> value.toInt()
            Integer::class.java -> value.toInt()
            Double::class.java -> value.toDouble()
            Long::class.java -> value.toLong()
            Date::class.java -> Date(value)
            else -> throw IllegalArgumentException("Can't auto-cast to $clazz. Add a converter as a second argument.")
        } as T
    }
}

fun <T> getAs(converter: (String) -> T): T {
    try {
        return converter(value)
    } catch (e: Exception) {
        // ...
    }
}