I have this inline reified function, is there a wa...
# getting-started
m
I have this inline reified function, is there a way for compiler to optimize away when branches other than the matching one (considering it's inline)
Copy code
suspend inline fun <reified T> get(key: String): T? =
        dao.getById(key)?.let {
            when (T::class) {
                String::class -> it.value as T
                Int::class -> it.value.toInt() as T
                Float::class -> it.value.toFloat() as T
                Double::class -> it.value.toDouble() as T
                else -> throw IllegalArgumentException("Not implemented for the given type.")
            }
        }
s
Take a look at the bytecode
m
I did, all when branches were there
e
File an improvement request to http://kotl.in/issue Explain your use-case there
m
it looks more general than I though:
Copy code
fun b(): String = when(String::class){
    Int::class -> "1"
    String::class -> "2"
    else -> "0"
}
Kinda silly example but one might expect compiler to optimize it to
fun b() = "2"
I've modified the issue to explain the broader issue and how it applies to inline reified functions.
👍 1