Consumer can't find in Kotlin

北城以北 提交于 2019-12-11 18:07:43

问题


I want to convert this Java example to kotlin.

But...

{responseBody -> .....} Type mismatch.

fun handleAudioMessageEvent(event: MessageEvent<AudioMessageContent>) {
    handleHeavyContent(
        event.replyToken,
        event.message.id
    ) { responseBody ->
        val provider = event.message.contentProvider
        val mp4: DownloadedContent
        if (provider.isExternal) {
            mp4 = DownloadedContent(null, provider.originalContentUrl)
        } else {
            mp4 = saveContent("mp4", responseBody)
        }
        reply(event.replyToken, AudioMessage(mp4.uri, 100))
    }
}
.
.
.
private fun handleHeavyContent(
    replyToken: String, messageId: String,
    messageConsumer: Consumer<MessageContentResponse>
) {
    val response: MessageContentResponse
    try {
        response = lineMessagingClient?.getMessageContent(messageId)
            ?.get()!!
    } catch (e: InterruptedException) {
        reply(replyToken, TextMessage("Cannot get image: " + e.message))
        throw RuntimeException(e)
    } catch (e: ExecutionException) {
        reply(replyToken, TextMessage("Cannot get image: " + e.message))
        throw RuntimeException(e)
    }

    messageConsumer.accept(response)
}

Type mismatch.

Required: Consumer

Found: (???) -> Unit


回答1:


If you change the declaration of handleHeavyContent to this, it will work I guess:

private fun handleHeavyContent(
    replyToken: String, messageId: String,
    messageConsumer: (MessageContentResponse) -> Unit
)


来源:https://stackoverflow.com/questions/57398575/consumer-cant-find-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!