Why does the compiler (IntelliJ) not accept this `...
# getting-started
b
Why does the compiler (IntelliJ) not accept this
fold
lambda? error is
Kotlin: Type mismatch: inferred type is Unit but Int was expected
Copy code
fun <T, Int> Iterable<T>.sum(tx: (T) -> Int): Int {
    return map { tx(it) }
        .also { it -> println(it) }
        .fold(0 as Int) { acc, next -> acc + next }
}
d
typo
<T, Int>
b
Thanks
s
By declaring
<T, Int>
you have actually made a new generic parameter called
Int
which is not the same as (and is hiding) the real
Int
type. If you just drop that generic parameter from the signature, it should work.
1
🙏 1