1
Kotlin有一个扩展功能run
。在Kotlin中使用运行函数而不是返回是否是一种好的做法?
/**
* Calls the specified function [block] and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <R> run(block:() -> R): R = block()
和run
函数可以用来代替返回。
// an example multi-line method using return
fun plus(a: Int, b: Int): Int {
val sum = a + b
return sum
}
// uses run instead of return
fun plus(a: Int, b: Int): Int = run {
val sum = a + b
sum
}
哪种风格更好?