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

Popular posts from this blog

vllm : Failed to infer device type

android studio kotlin source is null error

gemini cli getting file not defined error