1. Overview

In this quick tutorial, we’ll discuss how to deal with unused parameters in Kotlin.

2. Introduction to the Problem

Generally speaking, we should remove the unused parameters from the function signatures. However, sometimes we must keep them there for a while.

For instance, suppose our program is a central library and is a key dependency of many essential applications with big codebases. In this situation, if we had changed some functions’ signatures, it could lead to breaking changes on many applications simultaneously. We don’t want that to happen. Therefore, we’d first keep the unused parameters in the code.

We can still compile our code with the unused parameters. However, the Kotlin compiler warns us about them. Let’s see an example:

fun paramInFunction(num: Int, magic: String) {
    println("We only use the Int parameter: $num")
}

In the function above, we only use the num: Int parameter in the function body. The magic parameter is not used at all. Now, if we compile the code, we can see:

/.. path to the kotlin source file .../UnusedParameter.kt:3:31
Kotlin: Parameter 'magic' is never used

So next, let’s see how to mark the parameter as unused so that the Kotlin compiler doesn’t complain about that.

3. Unused Parameters in Functions

The @Suppress(“UNUSED_PARAMETER”) annotation allows us to suppress the unused parameter compiler warning. We can simply attach this annotation to the unused parameters, or we can attach them to the entire function:

fun paramInFunction2(num2: Int, @Suppress("UNUSED_PARAMETER") magic2: String) {
    println("We only use the Int parameter: $num2")
}

@Suppress("UNUSED_PARAMETER")
fun paramInFunction3(num3: Int, magic3: String) {
    println("We only use the Int parameter: $num3")
}

Now, if we compile the two functions above, the Kotlin compiler doesn’t print the “Parameter x is never used” warning message anymore.

So, @Suppress(“UNUSED_PARAMETER”) can mark unused parameters in a regular function.

However, sometimes we have unused parameters in lambda expressions. Next, let’s see how to deal with this case.

4. Unused Parameters in Lambda Expressions

First, let’s see an example of a lambda expression with an unused parameter:

listOf("Kotlin", "Java", "Python").forEachIndexed { idx, value -> println("We only print the index of the element: $idx") }

As the code above shows, the lambda expression in the forEachIndexed function only used the idx (Integer) parameter. If we compile the code, we’ll see the compiler warning:

/ .. path to the kotlin source file ... /UnusedParameter.kt:18:62
Kotlin: Parameter 'value' is never used, could be renamed to _ 

Here, we cannot attach the @Suppress(“UNUSED_PARAMETER”) annotation to the lambda expression:

listOf(...).forEachIndexed { idx, @Suppress("UNUSED_PARAMETER") value -> ...

This leads to a different compiler error:

Kotlin: Unexpected tokens.

So, @Suppress(“UNUSED_PARAMETER”) cannot help here.

Actually, the correct solution has been mentioned in the earlier compiler warning: “*could be renamed to _*“. In Kotlin, if we want to ignore a parameter in a lambda expression, we can name the parameter “*_*“:

listOf("Kotlin", "Java", "Python").forEachIndexed { idx, _ -> println("We only print the index of the element: $idx") }

Now, if we recompile the code, we see no more warnings.

5. Conclusion

In this article, we’ve learned how to mark unused parameters in Kotlin’s regular functions and lambda expressions.

As usual, all the examples are available over on GitHub.