简体   繁体   中英

How use Lambda handleHeavyContent() in Kotlin

Sample code: https://github.com/line/line-bot-sdk-java/blob/master/sample-spring-boot-kitchensink/src/main/java/com/example/bot/spring/KitchenSinkController.java

I try convert to Kotlin.

But responseBody is Type mismatch.

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

Required: Consumer

Found: (???) -> Unit

Hey I had the same problem so after some searches finally I came up with some ways to define a lambda in a function. In your case I would do something like this

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

Hope this helps you

Example fun with Unit and lamba use:


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


x(1) { param ->
   println(param) // -> gives 1
}

Your function, after refactoring, must be missing parameter for lamba

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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