{} that can be treated as a first-class value. It is not declared with the fun keyword, but rather instantiated as an object of a function type, allowing it to be assigned to variables, passed as an argument, or returned from higher-order functions.
Base Syntax
The full syntactic form of a lambda expression requires the parameters and the body to be enclosed within curly braces, separated by the-> operator.
- (Int, Int) -> Int: The explicit function type declaration.
{ ... }: The lambda literal.- a: Int, b: Int: The parameter list.
- ->: The arrow operator separating parameters from the body.
- a + b: The lambda body.
Type Inference
The Kotlin compiler can infer parameter types and return types based on the context. If the function type is explicitly declared on the variable, the parameter types inside the lambda literal can be omitted.Implicit it Parameter
When a lambda expression has exactly one parameter, the compiler allows the omission of the parameter declaration and the -> operator. The single parameter is implicitly exposed under the name it.
Return Values
Lambda expressions do not use thereturn keyword for standard execution flow. The evaluated result of the last expression inside the lambda body is implicitly treated as the return value.
return@label) must be used, typically referencing the function to which the lambda was passed.
Trailing Lambda Convention
If the last parameter of a function is a function type, a lambda expression passed as the corresponding argument can be placed outside the function’s parentheses.Unused Parameters
If a lambda has multiple parameters but not all are used within the body, the unused parameters can be replaced with an underscore_ to avoid compiler warnings and signal intent.
Closures
Kotlin lambdas support closures, meaning they can access and modify variables declared in the outer scope where the lambda is defined. Unlike Java, Kotlin allows the mutation of captured variables (they do not need to befinal or effectively final).
Destructuring in Lambdas
If a lambda parameter is a type that supports destructuring declarations (such asPair, Map.Entry, or a data class), it can be destructured directly in the parameter list by wrapping the variables in parentheses.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More





