简体   繁体   English

未使用 lambda 表达式

[英]The lambda expression is unused

While using Android's Switch , I was attaching a setOnCheckedChangeListener to it and got this warning在使用 Android 的Switch ,我给它附加了一个setOnCheckedChangeListener并收到了这个警告

The lambda expression is unused.未使用 lambda 表达式。 If you mean a block, you can use 'run {...}'如果你的意思是一个块,你可以使用'run {...}'

Here' the code snippet:这是代码片段:

switchAction.setOnCheckedChangeListener({
    _, isChecked ->
    {
        preferences.userStatus = isChecked
        switchToggleVisibility(isChecked)
        if (isChecked) {
            fetchStats()
            getOrders()
        } else {
            releaseOrder()
        }
    }
})

Using run does fix this warning, but does anyone know the cause behind this?使用run确实修复了这个警告,但有人知道这背后的原因吗? How is the lambda expression unused? lambda 表达式如何未使用?

You're mixing Java's lambda notation with Kotlin's lambda notation, creating a lambda that returns another nested lambda in this case.您将 Java 的 lambda 符号与 Kotlin 的 lambda 符号混合,创建一个 lambda,在这种情况下返回另一个嵌套的 lambda。 The correct and idiomatic syntax would look like this:正确且惯用的语法如下所示:

switchAction.setOnCheckedChangeListener { _, isChecked ->
    preferences.userStatus = isChecked
    switchToggleVisibility(isChecked)
    if (isChecked) {
        fetchStats()
        getOrders()
    } else {
        releaseOrder()
    }
}

Taking all the noise out, a normal lambda looks like this:去掉所有的噪音,一个正常的 lambda 看起来像这样:

{ arg1, arg2 -> returnValue } // Type: (A, B) -> C

You did this:你这样做了:

{ arg1, arg2 -> { returnValue } } // Type: (A, B) -> (() -> C)

Which could also be written like this:也可以这样写:

{ arg1, arg2 -> { -> returnValue } } // Type: (A, B) -> (() -> C)

This notation makes it a bit clearer that the lambda doesn't return the return value, but returns another lambda with no parameters that returns the return value.这种表示法更清楚地表明 lambda 不返回返回值,而是返回另一个没有返回返回值的参数的 lambda。

Usually, this would get caught by the compiler as a wrong return type, but in your case, the return value of the lambda is not used.通常,这会被编译器捕获为错误的返回类型,但在您的情况下,不使用 lambda 的返回值。 So, you're just creating the inner lambda without returning or running it, that's why you're getting a warning.因此,您只是在创建内部 lambda 而不返回或运行它,这就是您收到警告的原因。

Yes, the是的,该

_, isChecked ->
    { ... }

Must be changed to必须改为

_, isChecked ->
    preferences.userStatus = isChecked
    switchToggleVisibility(isChecked)
    if (isChecked) {
        fetchStats()
        getOrders()
    } else {
        releaseOrder()
    }

So just remove the curly braces because else you just create a block which isn't executed at all.因此,只需删除花括号,否则您只会创建一个根本不执行的块。 Alternatively you could also do或者你也可以做

_, isChecked ->
    run {
        preferences.userStatus = isChecked
        switchToggleVisibility(isChecked)
        if (isChecked) {
            fetchStats()
            getOrders()
        } else {
            releaseOrder()
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM