Is there a way to do something like this in Kotlin without reflection?
inline fun <reified T : kotlin.Enum<T>> safeValueOf(type: String?): T? { return java.lang.Enum.valueOf(T::class.java, type) }
The following example does not compile due to:
Enter the parameter parameter for T in inline fun <reified T : kotlin.Enum<T>> safeValueOf(type: kotlin.String?): T?
fails: deduced type TestEnum?
not a subtype of kotlin.Enum<TestEnum?>
enum class TestEnum fun main() { val value: TestEnum? = safeValueOf("test") }
source share