sumBy(selector) returns Int
sumByDouble(selector) returns Double
Why doesn't sumBy return Long? Is there a workaround for this?
That's a decision Kotlin team made. Since it's not possible to have return type overloads in Java the sumBy*
have to have different names depending on the return type.
It's easy enough to add your own sumByLong
though:
inline fun <T> Iterable<T>.sumByLong(selector: (T) -> Long): Long { var sum = 0L for (element in this) { sum += selector(element) } return sum }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With