kotlin特殊关键字,let、apply、also、with、run

Categories: kotlin

let

定义:

/**
 * Calls the specified function [block] with `this` value as its argument and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)

执行一个参数传入的函数,该函数的参数是当前对象。并且返回一个完全自由的结果。

典型用法
val answerToUniverse = strBuilder.let {
    it.append("Douglas Adams was right after all")
    it.append("Life, the Universe and Everything")
    42
}

apply

定义:

/**
 * Calls the specified function [block] with `this` value as its receiver and returns `this` value.
 */
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }

执行一个以参数传入的函数,该函数在当前对象上面执行,并返回当前对象

典型用法

also

定义:

/**
 * Calls the specified function [block] with `this` value as its argument and returns `this` value.
 */
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }

执行一个以参数传入的函数,函数的参数就是当前对象,并返回当前对象

典型用法

with

定义:

/**
 * Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()

不是一个扩展函数,相关对象作为参数传入,然后在传入对象上面执行特殊的函数,并且返回执行函数的返回值

典型用法

####run

定义:

/**
 * Calls the specified function [block] with `this` value as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> T.run(block: T.() -> R): R = block()

给当前对象执行一个函数,返回结果就是函数的返回结果,这个run还有一个不是扩展的版本

典型用法