sumOf - resolution ambiguity

Hi, I have an issue with the new sumOf methods in Kotlin 1.5 (language and api version are both set to 1.5).

Take this fairly simple example:

val iter = listOf<String>()
val total = iter.sumBy { if (it.isEmpty()) 5 else 0 }

The Kotlin plugin suggests changing sumBy to sumOf, but then it does this:

val iter = listOf<String>()
val total = iter.sumOf<T>({ if (it.isEmpty()) 5 else 0 })

which obviously doesn’t compile because of <T>. If I clean it up, it still doesn’t work:

val iter = listOf<String>()
val total = iter.sumOf { if (it.isEmpty()) 5 else 0 }

because of

Overload resolution ambiguity. All these functions match.
public inline fun <T> Iterable<TypeVariable(T)>.sumOf(selector: (TypeVariable(T)) → Int): Int defined in kotlin.collections
public inline fun <T> Iterable<TypeVariable(T)>.sumOf(selector: (TypeVariable(T)) → Long): Long defined in kotlin.collections

How are you supposed to use sumOf?

(I know you could rewrite the example code in a completely different way, but the point is that the code works with sumBy but doesn’t with sumOf, which seems like a pretty big issue if the plugin wants to automatically migrate all sumBy to sumOf.)

https://youtrack.jetbrains.com/issue/KT-46360

2 Likes