Maybe something like this? ```fun <T> getAs(...
# codereview
p
Maybe something like this?
Copy code
fun <T> getAs(clazz: Class<T>, converter: ((String) -> Any)? = null): T {
    try {
        val convertedValue =
            if (converter != null) converter(value)
            else 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.")
            }
        return convertedValue as T
    } catch (e: Exception) {
        // ...
    }
}