Syntax

Remarks

Input type parameters can be left out when they can be left out when they can be inferred from the context. For example say you have a function on a class that takes a function:

data class User(val fistName: String, val lastName: String) {

fun username(userNameGenerator: (String, String) -> String) = userNameGenerator(firstName, secondName)

}

You can use this function by passing a lambda, and since the parameters are already specified in the function signature there’s no need to re-declare them in the lambda expression:

val user = User("foo", "bar")
println(user.userName { firstName, secondName ->
     "${firstName.toUppercase}"_"${secondName.toUppercase}"
 }) // prints FOO_BAR

This also applies when you are assigning a lambda to a variable:

//valid:
val addition: (Int, Int) = { a, b -> a + b }
//valid:
val addition = { a: Int, b: Int -> a + b }
//error (type inference failure):
val addition = { a, b -> a + b }