https://kotlinlang.org logo
Title
b

Benoît Liessens

03/03/2023, 1:10 PM
Why does the compiler (IntelliJ) not accept this
fold
lambda? error is
Kotlin: Type mismatch: inferred type is Unit but Int was expected
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

Dominaezzz

03/03/2023, 1:16 PM
typo
<T, Int>
b

Benoît Liessens

03/03/2023, 1:17 PM
Thanks
s

Sam

03/03/2023, 1:37 PM
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.