Deb*_*jan 1 android higher-order-functions kotlin
在Kotlin反复面对这个问题
fun test(){
compute { foo -> Log.e("kotlin issue", "solved") } // This line is //showing error
}
fun compute(body: (foo:String) -> Unit?){
body.invoke("problem solved")
}
Run Code Online (Sandbox Code Playgroud)
传递给compute函数的lambda 必须返回Unit?.现在,您将返回Log.e()调用的结果,该结果返回Int表示写入输出的字节数.如果您要做的只是在lambda中记录一条消息,您可以Unit在它的末尾显式返回,如下所示:
fun test() {
compute { foo ->
Log.e("kotlin issue", "solved")
Unit
}
}
Run Code Online (Sandbox Code Playgroud)
另外,请参阅此问题,Unit讨论转换返回值的其他方法.
| 归档时间: |
|
| 查看次数: |
5482 次 |
| 最近记录: |