Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A lambda parameter in Kotlin is a variable declared within a lambda expression that receives an argument when the underlying function type is invoked. These parameters are defined on the left side of the arrow (->) operator, which separates the parameter list from the execution body.

Explicit Parameter Declaration

By default, lambda parameters are declared explicitly with their names and types.
val multiplyExplicit: (Int, Int) -> Int = { x: Int, y: Int -> x * y }
If the Kotlin compiler can infer the parameter types from the surrounding context (such as the declared function type of the variable), the explicit type declarations can be omitted.
val multiplyInferred: (Int, Int) -> Int = { x, y -> x * y }

The Implicit it Parameter

When a lambda expression has exactly one parameter, and its type can be inferred by the compiler, Kotlin allows you to omit the parameter declaration and the -> operator entirely. The single parameter is implicitly exposed under the keyword it.
// Explicit declaration
val squareExplicit: (Int) -> Int = { number -> number * number }

// Implicit 'it' declaration
val squareImplicit: (Int) -> Int = { it * it }

Unused Parameters

If a lambda requires multiple parameters to satisfy a function type signature, but the lambda body does not utilize all of them, the unused parameters can be replaced with an underscore (_). This prevents compiler warnings and explicitly signals to other developers that the parameter is intentionally ignored.
val logMessage: (Int, String, Throwable) -> Unit = { _, message, _ -> 
    println(message) 
}

Destructuring Declarations

Lambda parameters support destructuring declarations. If a parameter is a type that provides componentN() operator functions (such as a Data Class, Pair, or Map.Entry), you can destructure the object directly within the parameter list by wrapping the destructured variables in parentheses.
// Standard parameter
val processPairStandard: (Pair<Int, String>) -> Unit = { pair ->
    println("${pair.first} and ${pair.second}")
}

// Destructured parameter
val processPairDestructured: (Pair<Int, String>) -> Unit = { (id, name) ->
    println("$id and $name")
}
You can also combine destructuring with the unused parameter underscore if you only need specific components of the destructured object:
val processPairPartial: (Pair<Int, String>) -> Unit = { (_, name) ->
    println("Only using $name")
}

Default Values Restriction

Unlike standard function parameters, lambda parameters cannot have default values. The Kotlin compiler strictly enforces this rule, meaning all arguments must be provided when the lambda’s underlying function type is invoked. Attempting to assign a default value to a lambda parameter results in a syntax error.
// INVALID SYNTAX: Compiler error "Lambda parameters cannot have default values"
// val increment: (Int) -> Int = { x: Int = 1 -> x + 1 }

Shadowing

Lambda parameters follow standard lexical scoping rules. If a lambda parameter shares a name with a variable in the enclosing scope, the lambda parameter will shadow the outer variable within the lambda body.
val x = 10
val addToX: (Int) -> Int = { x -> 
    x + 5 // 'x' refers to the lambda parameter, not the outer variable
}
Master Kotlin with Deep Grasping Methodology!Learn More