How is the new sumOf() function in Kotlin 1.4 different from sumBy() or sumByDouble() functions?
What's the difference between sumOf() and sumBy() in Kotlin 1.4?
4.9k Views Asked by Mahozad At
2
There are 2 best solutions below
0
On
As the Kotlin 1.4 changelog states:
...
sumOflets you handle sums of different types in the same way. it produces sums of the typesInt,Long,Double, ... [depending on the return type of the given lambda].
TLDR: there's no difference, and eventually
sumBy/sumByDoublecan be deprecated in favor of the singlesumOf.sumOfoperation has overloads for different numeric types returned from the selector function. It's something that was impossible previously with the old type inference facility, so in order to handle different numeric types it was required to have functions with the different names:sumByto sum ints,sumByDoubleto sum doubles, etc.When the new inference has finally arrived with the experimental support of overload resolution by selector lambda return type, we've decided not to overload the existing
sumByfunction, but to introduce a new operationsumOf. This way it has less chance of breaking something and is more consistent in naming with the new operationslist.minOf/maxOf { selector }.