Underscore for unused variables
In some cases, we define a lambda expression that does not use all its parameters. When we leave them named, they might be distracting to a programmer who is reading the lambda expression and trying to understand its purpose. Let's look at the function that filters every second element. The second parameter is the element value, and it is unused in this example:
list.filterIndexed { index, value -> index % 2 == 0 }
To prevent a misunderstanding, there are some conventions used, such as ignoring the parameter names:
list.filterIndexed { index, ignored -> index % 2 == 0 }
Because these conventions were unclear and problematic, Kotlin introduced underscore notation, which is used as a replacement for the names of parameters that are not used:
list.filterIndexed { index, _ -> index % 2 == 0 }
This notation is suggested, and there is a warning displayed when a lambda expression parameter is unused: