kotlin - Trailing lambda syntax
This is really an eye opening moment for me to see this in Kotlin - where we can pass the last parameter in a function body. It is call trailing lambda syntax. In the example, I am passing the value "10" like this to the function sayHello.
fun main() {
println("Hello, world!!!")
sayHello() { #1 way to call it
10
}
sayHello { 10} #2 another way to call it
}
fun sayHello(name: String = "jeremy", value: () -> Int) {
println("$name: ${value()}")
}
It is a much cleaner way to call it.
Comments