Basic Lambdas

suggest change

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 }

When the lambda takes one parameter, and the type can be inferred from the context, you can refer to the parameter by it.

listOf(1,2,3).map { it * 2 } // [2,4,6]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents