Kotlin Lambda作为参数的功能

发布于 2025-01-22 05:43:49 字数 1530 浏览 5 评论 0原文

我是Kotlin的新手,很难理解下面的代码

private fun <T> catchAsyncExceptions(f: () -> CompletableFuture<T>) =
    try {
        f().get()
    } catch (e: ExecutionException) {
        throw e.cause!!
    }

,因此该功能称为Catchasyncexceptions,其输入参数是一个称为f的函数,它是>() - &gt ;完整的future&lt; t&gt; 因此,我认为您使用它,

catchAsyncExceptions(someFunctionThatTakesNoArgumentAndReturnsCompletableFuture)

但是我看到用法是

override fun getUserInfo(userId: String) =
    catchAsyncExceptions {
        membersClient.getUserLocation(
            GetUserLocationRequest(userId)
        )
            .thenApply { response ->
                val (success, error) = parseSuccessAndError<GetUserLocationResponseResult.Success>(response.result!!)
                error?.let {
                    UserInfoResponse(
                        error = error.code
                        )
                    )
                } ?: run {
                    UserInfoResponse(
                        data = UserInfoResponseDto(
                            location = success?.success?.location.toString(),
                        )
                    )
                }
            }
    }

membersClient.getUserLocation(
            GetUserLocationRequest(userId)
        )

返回pountableFuture类型

我特别困惑为什么它是卷发括号而不是括号

catchAsyncExceptions {
...
}

I am new to Kotlin and have difficulty understand the code below

private fun <T> catchAsyncExceptions(f: () -> CompletableFuture<T>) =
    try {
        f().get()
    } catch (e: ExecutionException) {
        throw e.cause!!
    }

So this function is called catchAsyncExceptions, its input parameter is a function called f which is () -> CompletableFuture<T>
So I would think that you use it by

catchAsyncExceptions(someFunctionThatTakesNoArgumentAndReturnsCompletableFuture)

However I see the usage is

override fun getUserInfo(userId: String) =
    catchAsyncExceptions {
        membersClient.getUserLocation(
            GetUserLocationRequest(userId)
        )
            .thenApply { response ->
                val (success, error) = parseSuccessAndError<GetUserLocationResponseResult.Success>(response.result!!)
                error?.let {
                    UserInfoResponse(
                        error = error.code
                        )
                    )
                } ?: run {
                    UserInfoResponse(
                        data = UserInfoResponseDto(
                            location = success?.success?.location.toString(),
                        )
                    )
                }
            }
    }

Note that

membersClient.getUserLocation(
            GetUserLocationRequest(userId)
        )

returns CompletableFuture type

I am especially confused why it was a curly bracket rather than a bracket

catchAsyncExceptions {
...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2025-01-29 05:43:50

在Kotlin中,当您具有lambda函数作为参数时,括号是完全可选的。您可以将实现重写为:

catchAsyncExceptions({
    membersClient.getUserLocation(
        GetUserLocationRequest(userId)
    )
        .thenApply({ response ->
            val (success, error) = parseSuccessAndError<GetUserLocationResponseResult.Success>(response.result!!)
            error?.let({
                UserInfoResponse(
                    error = error.code
                    )
                )
            }) ?: run({
                UserInfoResponse(
                    data = UserInfoResponseDto(
                        location = success?.success?.location.toString(),
                    )
                )
            })
        })
})

这是一个完美工作的代码。为简单起见,省略括号以使代码更可读。

In Kotlin, when you have a lambda function as a parameter, the brackets are completely optional. You can rewrite the implementation as:

catchAsyncExceptions({
    membersClient.getUserLocation(
        GetUserLocationRequest(userId)
    )
        .thenApply({ response ->
            val (success, error) = parseSuccessAndError<GetUserLocationResponseResult.Success>(response.result!!)
            error?.let({
                UserInfoResponse(
                    error = error.code
                    )
                )
            }) ?: run({
                UserInfoResponse(
                    data = UserInfoResponseDto(
                        location = success?.success?.location.toString(),
                    )
                )
            })
        })
})

and this is a perfectly working code. For simplicity, the brackets are omitted to make the code more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文