Tim McCormack
12/08/2019, 2:27 PMFunction
from the Java stdlib and the stream API:
import java.util.function.Function
listOf(1, 2, 3).stream().map(Function<Int, Int> { it * it })
listOf(1, 2, 3).stream().map { it * it }
I can either use the SAM object syntax or the trailing lambda syntax. Makes sense.
But then, if I make a fun that accepts a Function, the compiler will only let me use the SAM object syntax!
fun takesSAM(x: Int, f: Function<Int, Int>): Int = f.apply(x)
takesSAM(5, Function { it * it })
takesSAM(5) { it * it } // compiler error