我使用 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
)
行为上没有差异。从可读性的角度来看,第一个是迟钝且过于复杂的。