nkiesel
08/26/2021, 8:35 PM(1..10).sumOf { if (it % 2 == 0) 1 else 0 }
. This looks like a bug to me (I tried both 1.5.21 and 1.5.30). I can avoid that using e.g. (1..10).sumOf { if (it % 2 == 0) 1L else 0L }
or (1..10).sumOf { if (it % 2 == 0) 1.toInt() else 0.toInt() }
, but the first one produces a Long
instead of an Int
and the 2nd one is just ugly.ephemient
08/26/2021, 8:47 PMephemient
08/26/2021, 8:52 PMval sum: Int = sumOf { ... }
that will worknkiesel
08/26/2021, 9:39 PMval sum: Int = (1..10).sumOf { if (it % 2 == 0) 1 else 0 }
still fails with the same error.
My personal belief is that the decision to support automatic promotions from Int
literals to Long
is a design mistake in Kotlin. Kotlin normally (and rightfully so) requires explicit conversions in all other places. So why then should I be allowed to use val l: Long = 1
and not be forced to use val l: Long = 1L
?ephemient
08/26/2021, 9:53 PM