Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark unused parameters in Kotlin

I am defining some functions to be used as callbacks and not all of them use all their parameters.

How can I mark unused parameters so that the compiler won't give me warnings about them?

like image 330
TheTeaMan Avatar asked Mar 14 '15 07:03

TheTeaMan


People also ask

How do you turn off warnings on Kotlin?

You can also use @Suppress in Kotlin or @SuppressWarnings in Java. To suppress a lint warning with a comment, add a //noinspection id comment on the line before the statement with the error.

What is unused parameter?

Reports the parameters that are considered unused in the following cases: The parameter is passed by value, and the value is not used anywhere or is overwritten immediately. The parameter is passed by reference, and the reference is not used anywhere or is overwritten immediately.

How do you stop unchecked cast Kotlin?

1 Answer. Show activity on this post. Adding @Suppress("UNCHECKED_CAST") (also possible through IDEA's Alt + Enter menu) to any of statement, function, class and file should help.


2 Answers

With the @Suppress annotation You can suppress any diagnostics on any declaration or expression.

Examples: Suppress warning on parameter:

fun foo(a: Int, @Suppress("UNUSED_PARAMETER") b: Int) = a 

Suppress all UNUSED_PARAMETER warnings inside declaration

@Suppress("UNUSED_PARAMETER") fun foo(a: Int,  b: Int) {   fun bar(c: Int) {} }  @Suppress("UNUSED_PARAMETER") class Baz {     fun foo(a: Int,  b: Int) {         fun bar(c: Int) {}     } } 

Additionally IDEA's intentions(Alt+Enter) can help you to suppress any diagnostics:

like image 60
bashor Avatar answered Sep 29 '22 08:09

bashor


If your parameter is in a lambda, you can use an underscore to omit it. This removes the unused parameter warnings. It will also prevent IllegalArgumentException in the case that the parameter was null and was marked non-null.

See https://kotlinlang.org/docs/reference/lambdas.html#underscore-for-unused-variables-since-11

like image 31
Mike Avatar answered Sep 29 '22 08:09

Mike