uli
05/31/2017, 7:30 PMtypealias NumberType = Double
inline fun Number.toNumberType() = when (NumberType::class) {
Double::class -> this.toDouble() as NumberType
Float::class -> this.toFloat() as NumberType
Long::class -> this.toLong() as NumberType
Int::class -> this.toInt() as NumberType
Short::class -> this.toShort() as NumberType
Byte::class -> this.toByte() as NumberType
else -> throw IllegalArgumentException("NumberType must be in [Double, Float, Long, Int, Short, Byte]")
}
Looking at the decompiled java code, it generates all when
branches, not noticing, that only one will ever be selected.
Is this expected or should i report?
Slack Conversationorangy
dmitry.petrov
06/01/2017, 10:50 AMtypealias NumberType = Double
typealias NumberHelper = DoubleHelper
object DoubleHelper {
fun Number.toNumberType() = toDouble()
}
orangy
dmitry.petrov
06/01/2017, 10:52 AMdmitry.petrov
06/01/2017, 11:00 AMdmitry.petrov
06/01/2017, 11:03 AMuli
06/01/2017, 4:43 PMuli
06/01/2017, 4:47 PMdmitry.petrov
06/01/2017, 5:22 PMuli
06/02/2017, 8:29 AMtypealias NumberType = Double
inline fun Number.toNumberType() = numericConvert(this, NumberType::class)
inline fun numericConvert(value: Number, type: KClass<Double>) = value.toDouble()
inline fun numericConvert(value: Number, type: KClass<Float>) = value.toFloat()
inline fun numericConvert(value: Number, type: KClass<Long>) = value.toLong()
inline fun numericConvert(value: Number, type: KClass<Int>) = value.toInt()
inline fun numericConvert(value: Number, type: KClass<Short>) = value.toShort()
inline fun numericConvert(value: Number, type: KClass<Byte>) = value.toByte()
But still with optimization in place both should compile down to toDouble()