kotlin,从释放内部返回的地方

lan*_*nyf 1 let kotlin

@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block(this)
}
Run Code Online (Sandbox Code Playgroud)

并具有如下功能:

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
}
Run Code Online (Sandbox Code Playgroud)

let go{} 中块内的 return 在哪里,退出fun getType()或只是从 中返回let{}

gid*_*dds 10

Kotlin 中的规则是从代码中最近的封闭区域返回普通数据。returnfun

\n

请参阅此处的语言文档。

\n

如果有一个封闭的 lambda,那么只有当 lambda 是内联的(即传递给用关键字标记的函数inline)时才可能;否则编译器会抱怨。

\n

如果需要,您可以通过使用封闭标签(例如return@myLabel)或函数名称(例如return@let)限定返回值来更改它。\xc2\xa0 但如果没有限定,您只需查找使用 定义的最近的封闭函数fun

\n