这些 Kotlin 函数等价吗?

问题描述 投票:0回答:1

我使用 Kotlin 的时间不长,但我遇到了使用这些模式的代码库,我想知道这 3 个扩展函数是否等效,或者行为上是否存在一些细微的差异?

fun EntityObject.transform(): DtoObject {
    this.apply {
        return DtoObject(
            id = id,
            description = label
        )
    }
}
fun EntityObject.transform(): DtoObject {
    return DtoObject(
        id = id,
        description = label
    )
}
fun EntityObject.transform() = DtoObject(
        id = id,
        description = label
)
kotlin extension-methods
1个回答
0
投票

行为上没有差异。从可读性的角度来看,第一个是迟钝且过于复杂的。

© www.soinside.com 2019 - 2024. All rights reserved.