@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
并具有如下功能:
fun getType() : String? {
val type = mContent.let {
if (!TextUtils.isEmpty(it) && it == "TYPE_1") {
return "TYPE_A" . //where it returns to, as the result of the let{}, or as return value to exit the fun getType()?
}
else {
return it
}
}
if (type == "TYPE_A") {
return getType_A()
}
return type
}
放开{},退出
fun getType()
或只是从let{}
返回?
Kotlin 中的规则是 a plain
return
从代码中最近的封闭 fun
返回。
查看语言文档here.
如果有一个封闭的 lambda,那么只有当 lambda 是内联的(即传递给标有
inline
关键字的函数)时才有可能;否则编译器会抱怨。
如果需要,您可以通过使用封闭标签(例如
return@myLabel
)或函数名称(例如return@let
)限定返回来更改它。但是如果不合格,你只需要寻找最近的用fun
定义的封闭函数。