What does the underscore `_` mean in a lambda parameter list, and when would you use it?
answer
- _ = intentionally ignored parameter slot
- Avoids unused-variable noise and fake names
- Works positionally and inside ( ) destructuring
- _ is write-only: cannot be referenced
- Repeatable: { _, _, c -> c }
basics
~20 sWriting _ 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 }.
solid answer
~40 sIn a lambda parameter list, `_` is a placeholder for a parameter you intend to **ignore**. Instead of `{ key, value -> value }` (which names an unused `key`), you write `{ _, value -> value }`. It signals intent clearly and avoids 'unused variable' style warnings/lint noise. You can use `_` for any positional parameter you don't need, and even multiple times in the same list: `{ _, _, third -> third }`. It also works inside **destructuring**: `{ (_, value) -> value }` ignores the first component. `_` is not a real variable — you cannot read from it, and there's nothing to reference. It's available since Kotlin 1.1. Note it's distinct from the trailing-lambda or `it` features; it's purely about discarding named positional parameters.
code
kotlin · 8 lines// Ignore the key, keep the value
val values = mapOf("a" to 1, "b" to 2).map { (_, v) -> v } // [1, 2]
// Ignore index in a two-arg callback
listOf("x", "y").forEachIndexed { _, s -> println(s) }
// Multiple discards
val third = listOf(Triple(1, 2, 3)).map { (_, _, c) -> c } // [3]go deeper
Knows _ marks a parameter as unused and can apply it in a simple two-parameter lambda.
Uses _ in destructuring and multi-parameter lambdas, knows it is unreferenceable, and explains the readability/lint benefit.
Frames _ as an intent signal, relates it to the broader underscore convention (for-loops, multiple discards) and idiomatic style.
Promotes consistent discard conventions in code-review standards to keep callbacks self-documenting across a codebase.
## What `_` does When a lambda receives a parameter you don't plan to use, naming it forces you to invent a name and may trigger an 'unused parameter' lint. Kotlin lets you write **`_`** (a single underscore) instead, marking the parameter as intentionally **unused**: ```kotlin mapOf("a" to 1, "b" to 2).forEach { _, value -> println(value) } ``` Here the key is ignored cleanly. ## Where you can use it - **Any positional parameter** in a multi-parameter lambda: `{ _, b -> b }`. - **Multiple at once**: `{ _, _, c -> c }` ignores the first two. - **Inside destructuring**: `{ (_, value) -> value }` skips the first `componentN()` result. This is common with `Map.Entry` or data classes where you only want one field. ```kotlin data class User(val id: Int, val name: String) listOf(User(1, "Ann")).map { (_, name) -> name } // ["Ann"] ``` ## Rules and gotchas - `_` is **not a readable variable**. You cannot reference `_` in the body — there is nothing bound to it. It's purely a discard slot. - You can repeat `_` freely; each one is independent and ignored. - For a **single**-parameter lambda you'd normally just use `it`, or rely on the implicit form; `_` shines when there are multiple parameters and only some matter, or in destructuring. - It is the same underscore convention used in `for ((_, v) in map)` destructuring loops — a consistent 'I don't care about this slot' marker. ## Why it matters Using `_` is about **intent and noise reduction**: the reader instantly sees which inputs are irrelevant, and you avoid bogus names like `unused` or `ignored`. It's a small but idiomatic readability tool. ## Summary `_` discards an unwanted positional or destructured lambda parameter; it's write-only (unreferenceable), repeatable, and signals intent. Available since Kotlin 1.1.
- Can you read the value of a `_` parameter later in the lambda body?No. `_` is a discard placeholder, not a binding, so there is nothing to reference. If you need the value, give it a real name.
- Does `_` also work in non-lambda destructuring, like a for-loop?Yes. The same convention applies: `for ((_, value) in map) { ... }` ignores the key. It's a consistent 'unused slot' marker across destructuring sites.
_ is like leaving a seat empty at a table — it holds the position so others line up correctly, but no one sits there.
saying these in an interview costs you the question
- Trying to reference `_` in the body
- Thinking `_` is a special variable holding a value
- Not knowing `_` works inside destructuring `( )`
- Believing you can only use `_` once per lambda
- Confusing `_` with `it`