skip to content

Lambda Syntax & Implicit it

Lambdas are written in braces, and a single parameter can stay unnamed as it. Knowing the small extras — destructured parameters, _ for the ones you ignore — is what keeps chained collection code readable.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

What is the basic brace syntax of a Kotlin lambda, and what does the implicit `it` refer to?

level: juniorimportance: must knowfreq 85%

answer

  1. { params -> body }, arrow separates params from body
  2. it = auto-name for the single, undeclared parameter
  3. it is NOT a keyword and NOT this
  4. no it for zero or 2+ params
  5. name it explicitly when nested or unclear

basics

~20 s

A lambda is code in curly braces: { x -> body }. When it takes exactly one parameter you can skip naming it and use the built-in name it instead, like list.map { it * 2 }.

solid answer

~40 s

A Kotlin lambda literal is written `{ params -> body }`. Parameters come before the `->` arrow; the body is everything after it. When the lambda has exactly one parameter and you don't declare it explicitly, Kotlin generates the implicit name `it` for that parameter — so `list.map { it * 2 }` is shorthand for `list.map { x -> x * 2 }`. `it` only exists when there is a single parameter and no explicit parameter list; with zero or two-plus parameters it is unavailable and you must name them. The last expression of the body is the lambda's return value. `it` is purely a naming convenience provided by the compiler, not a special object or keyword like `this`.

code

kotlin · 6 lines
kotlin
// Explicit parameter vs implicit it — equivalent
val a = listOf(1, 2, 3).map { n -> n * 2 } // [2, 4, 6]
val b = listOf(1, 2, 3).map { it * 2 }     // [2, 4, 6]

// Two params: it is NOT available, must name them
val sums = listOf(1 to 2, 3 to 4).map { (x, y) -> x + y }

go deeper

for a junior

Can write { it * 2 } and explain that it is the single parameter and -> separates params from the body.

for a middle

Knows it is compiler-generated sugar available only for one undeclared parameter, not a keyword, and equals naming the param explicitly.

for a senior

Articulates readability trade-offs: when to drop it, the nested-lambda shadowing trap, and that the last expression is the result.

for a principal

Frames it as a naming-convention decision in API/style guidelines and weighs it against explicit naming for maintainable codebases.

## What a lambda literal is A **lambda** in Kotlin is an anonymous function written inline using curly braces. Its general form is: ```kotlin { parameterList -> body } ``` - **parameterList**: zero or more comma-separated parameter names, optionally typed. - **`->`**: the arrow that separates parameters from the body. If there are no parameters, you omit both the params and the arrow: `{ doSomething() }`. - **body**: one or more statements; the value of the **last expression** becomes the lambda's result. Example with an explicit parameter: ```kotlin val doubled = listOf(1, 2, 3).map { n -> n * 2 } // [2, 4, 6] ``` ## The implicit `it` When a lambda has **exactly one parameter** and you do **not** declare a parameter list (no `name ->`), the compiler auto-generates a parameter named **`it`**: ```kotlin val doubled = listOf(1, 2, 3).map { it * 2 } // it is each element ``` Key facts about `it`: - It is a normal parameter name supplied for convenience — **not** a keyword and **not** the same as `this`. - It is available **only** for single-parameter lambdas with no explicit arrow. The moment you write `n ->`, `it` is no longer in scope; you use `n`. - For **zero-parameter** lambdas there is no `it`. For **two-or-more** parameters you must name them: `{ a, b -> a + b }` — there is no `it`. - You can always replace `it` with an explicit name. Doing so is recommended when the lambda is long, nested, or the meaning is unclear. ## When NOT to use `it` In **nested** lambdas both default to `it`, which shadows the outer one and is confusing: ```kotlin rows.forEach { row -> row.cells.forEach { cell -> render(row, cell) } } // name them ``` ## Summary `{ x -> ... }` is the full form; `it` is the auto-named single parameter you get when you omit the parameter list. It is sugar, available only for one-parameter lambdas.

  • Is `it` a reserved keyword in Kotlin?
    No. It is an ordinary identifier the compiler supplies for an unnamed single lambda parameter. You can even declare your own variable named `it`, though it's discouraged because it shadows the implicit one.
  • Can you use `it` in a lambda that takes two parameters?
    No. `it` only exists for a single, undeclared parameter. With two or more parameters you must write an explicit list like `{ a, b -> ... }`.

it is like saying 'the thing' when there's only one thing in the room — unambiguous, so you don't bother naming it.

saying these in an interview costs you the question

  • Calling `it` a keyword like `this` or `super`
  • Thinking `it` works for multi-parameter lambdas
  • Believing `it` is a special object rather than just a parameter name
  • Not knowing the `->` separates params from body
  • Claiming the lambda returns its first expression instead of its last

context

open as a page

How do you destructure a parameter inside a lambda, and what are the rules and limits of destructuring lambda parameters?

level: middleimportance: should knowfreq 60%

basics

~20 s

If a lambda parameter is something like a Pair or a data class, you can split it into its parts inside parentheses, e.g. map.forEach { (key, value) -> ... }, instead of using one name and calling .first/.second.

open as a page

What does the underscore `_` mean in a lambda parameter list, and when would you use it?

level: middleimportance: should knowfreq 50%

basics

~20 s

Writing _ as a lambda parameter name means 'I won't use this one.' It's a placeholder so you don't have to invent a name for a value you ignore, like { _, value -> value }.

open as a page

What goes wrong when you nest lambdas that both rely on the implicit `it`, and how should you handle it?

level: seniorimportance: should knowfreq 45%

basics

~10 s

If a lambda inside another lambda both use it, the inner it hides the outer one, so you can't reach the outer value and the code is confusing. Fix it by naming the parameters.

open as a page

How does Kotlin infer the type of `it` (or named lambda parameters), and how can you make the parameter type explicit when needed?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Kotlin figures out the type of it from the function you pass the lambda to (its expected type). If that's unclear, you can write the type yourself, like { it: Int -> it * 2 }.

open as a page