I’m trying out having a “factory” function like this. (The reason being that it’s used in generated code and the generator should stay simple).
inline fun <reified T: Any> get(str: String): T =
when (T::class) {
Uri::class -> Uri.parse(str) as T
else -> throw IllegalStateException("don't know how to $str")
}
val x = get<Uri>("www.example.com")
That works.
Now I also want to add Enums
:
inline fun <reified T: Any> get(str: String): T =
when (T::class) {
Uri::class -> Uri.parse(str) as T
Enum::class -> java.lang.Enum.valueOf(T::class.java, str)
else -> throw IllegalStateException("don't know how to $str")
}
This does not work:
Any hints on such a use-case?