I have a question about trailing lambdas, and unde...
# announcements
t
I have a question about trailing lambdas, and under what circumstances they can be used. Take this code for instance, using
Function
from the Java stdlib and the stream API:
Copy code
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!
Copy code
fun takesSAM(x: Int, f: Function<Int, Int>): Int = f.apply(x)

takesSAM(5, Function { it * it })
takesSAM(5) { it * it } // compiler error