Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't sumBy(selector) return Long?

Tags:

kotlin

sumBy(selector) returns Int

sumByDouble(selector) returns Double

Why doesn't sumBy return Long? Is there a workaround for this?

like image 473
Jon Peterson Avatar asked May 31 '16 04:05

Jon Peterson


1 Answers

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 } 
like image 54
miensol Avatar answered Sep 21 '22 16:09

miensol