Pavlo Liapota
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) { // ... } }
A modern programming language that makes developers happier.