Kotlin 1.4中的sumOf()和sumBy()有什么区别? [英] What's the difference between sumOf() and sumBy() in Kotlin 1.4?

查看:685
本文介绍了Kotlin 1.4中的sumOf()和sumBy()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kotlin 1.4中新的 sumOf()函数与 sumBy() sumByDouble()函数有何不同?

解决方案

TLDR :没有区别,最终 sumBy / sumByDouble 可以不赞成使用单个 sumOf .

对于从选择器函数返回的不同数字类型,

sumOf 操作具有重载.使用旧的类型推断工具以前是不可能做到的,因此为了处理不同的数字类型,需要具有不同名称的函数: sumBy 求和整数, sumByDouble 加倍,等等.

当新的推断最终得到选择器lambda返回类型的重载解析的实验支持时,我们决定不重载现有的 sumBy 函数,而是引入一个新的操作 sumOf .这样,它破坏某些内容的机会就更少了,并且在使用新操作 list.minOf/maxOf {选择器} 命名时更加一致.

How is the new sumOf() function in Kotlin 1.4 different from sumBy() or sumByDouble() functions?

解决方案

TLDR: there's no difference, and eventually sumBy/sumByDouble can be deprecated in favor of the single sumOf.

sumOf operation 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: sumBy to sum ints, sumByDouble to 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 sumBy function, but to introduce a new operation sumOf. This way it has less chance of breaking something and is more consistent in naming with the new operations list.minOf/maxOf { selector }.

这篇关于Kotlin 1.4中的sumOf()和sumBy()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆