【问题标题】:How use Lambda handleHeavyContent() in Kotlin如何在 Kotlin 中使用 Lambda handleHeavyContent()
【发布时间】:2019-08-07 07:58:58
【问题描述】:

示例代码:https://github.com/line/line-bot-sdk-java/blob/master/sample-spring-boot-kitchensink/src/main/java/com/example/bot/spring/KitchenSinkController.java

我尝试转换为 Kotlin。

但是 responseBody 的类型不匹配。

    handleHeavyContent(
        event.replyToken,
        event.message.id
        ) {responseBody ->
    }

必填: 消费者

找到:(???) -> 单位

【问题讨论】:

    标签: java kotlin line-api


    【解决方案1】:

    嘿,我遇到了同样的问题,所以经过一番搜索,我终于想出了一些在函数中定义 lambda 的方法。 在你的情况下,我会做这样的事情

    fun handleHeavyContent(
        event.replyToken,
        event.message.id,
        response : (ResponseBody) -> Unit){
         //do your code and get the response body and pass it to the variable
         // get the body from a function or object and then use it like this
         val body : ResponseBody //initialize it here
         response(body)
        }
    

    希望对你有帮助

    【讨论】:

    【解决方案2】:

    使用 Unit 和 Lamba 的示例乐趣:

    
    fun x(
        val a: Int,
        val b: (param: Int) -> Unit
    ) {
        // any code
        b.invoke(a)
    }
    
    
    x(1) { param ->
       println(param) // -> gives 1
    }
    

    你的函数,在重构之后,一定是缺少 Lamba 的参数

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2023-01-25
    • 1970-01-01
    相关资源
    最近更新 更多