Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a cast from Double to <T : Number> possible, but not from Double to Int?

In the following, I have a generic function fun <T : Number> sum(list : List<T>) : T with a type parameter T : Number.

In the function, I sum up the list's numbers as a sum : Double and cast the sum in the end with return sum as T.

For example, if a list of Int is passed, I also get back an Int - and this works.

fun <T : Number> sum(list : List<T>) : T {
    var sum = 0.0
    for(x in list)
        sum += x.toDouble()
    return sum as T
}
fun main() { println(sum(listOf(1,2,3))) } // prints 6

Yet, the following does not work, and I am wondering why the generic functions above works but directly casting a Double to an Int does not.

fun main() {        
    val d : Double = 6.0
    val i = d as Int // java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
    println(i)
}

I have to admit that I expected both cases to fail, but surprisingly, the generic function works and I don't know why.

So the question is: Why does the generic function work and does not throw a ClassCastException when casting from Double to Int?

like image 344
Markus Weninger Avatar asked Dec 03 '21 11:12

Markus Weninger


People also ask

What happens when you cast a double to an int?

Since double is bigger data type than int, you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to a 32-bit integer, anything after the decimal point is lost.

How to round double into int in Java?

round() Math. round() accepts a double value and converts it into the nearest long value by adding 0.5 to the value and truncating its decimal points. The long value can then be converted to an int using typecasting.


2 Answers

Notice that in the first code snippet, which "works", you are not actually casting the result to Int. If you are using IntelliJ, it should have marked the cast as an "unchecked cast". This means that at runtime, it is not checked whether sum can actually be converted to type T. It only checks that sum is a Number, and that's it. Nothing else is performed.

You can see that the returned value is still a Double, not an Int by printing:

println(sum(listOf<Int>(1,2,3)) is Int) // false
println(sum(listOf<Int>(1,2,3)) is Double) // true

And as other answers explain, this is because of type erasure.

The reason why you still see 6, but not 6.0 is bit more involved. The Kotlin compiler sees that the sum call here should return an Int (at this point the types have not been erased yet), so it finds the println overload that takes an Int, which inlines to the Java System.out.prinln(int) method. To call this method, the compiler must generate code that converts the type-erased Number that sum returns to an int, so it calls Number.intValue.

Therefore, this is what's generated:

  33: invokestatic  #69     // Method sum:(Ljava/util/List;)Ljava/lang/Number;
  36: invokevirtual #73     // Method java/lang/Number.intValue:()I
  39: invokevirtual #79     // Method java/io/PrintStream.println:(I)V

If you had forced the compiler to call pritnln(Any), then it would print 6.0:

val any: Any = sum(listOf<Int>(1,2,3))
println(any)
like image 67
Sweeper Avatar answered Dec 26 '22 22:12

Sweeper


This is because the type erasure. In your case the generic info is only available during compile. At runtime the sum as T does noting, because it's not clear what T is. For example it's not possible to print out the type of T. It's an unchecked cast. You could also change the T type from Number to String, which makes no sense - but it would compile. So the function will not trow the ClassCastException because actual it does no cast. If you change the sum function to a refined type, which preserves the type info at runtime, it will do the cast and throw the error:

inline fun <reified  T : Number> sum(list: List<T>): T {
    var sum = 0.0
    for (x in list)
        sum += x.toDouble()
    return sum as T // java.lang.ClassCastException
}
like image 33
Tobse Avatar answered Dec 26 '22 22:12

Tobse