在 Kotlin 中标记未使用的参数 [英] Mark unused parameters in Kotlin

查看:38
本文介绍了在 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?

推荐答案

使用 @Suppress 注释,您可以禁止对任何声明或表达式进行任何诊断.

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

示例:禁止参数警告:

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

禁止声明中的所有 UNUSED_PARAMETER 警告

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) {}
    }
}

此外,IDEA 的意图(Alt+Enter)可以帮助您抑制任何诊断:

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

这篇关于在 Kotlin 中标记未使用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆