apply
/**
* Calls the specified function [block] with `this` value as its receiver and returns `this` value.
*
* For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#apply).
*/
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
return this
}
我們從源碼中可以知道 apply接受一個(gè)對象,返回一個(gè)對象崩瓤。閉包中使用(this)指代對象
一般使用于成員對象的操作
官方文檔對其解釋為 apply the following assignments to the object.
class StudyOne {
var karl: String ?= null
var karlTwo: String ?= null
}
fun main(args: Array<String>) {
val studyOne = StudyOne().apply {
this.karl = "aa"
this.karlTwo = "bb"
}
println("studyOne: ${studyOne.karl}")
println("studyOne: ${studyOne.karlTwo}")
}
//運(yùn)行結(jié)果
//aa
//bb
參考文檔: apply
run
/**
* Calls the specified function [block] with `this` value as its receiver and returns its result.
*
* For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#run).
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.run(block: T.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
我們發(fā)現(xiàn) run跟apply的差別只是apply返回對象本身伪货,run一個(gè)返回lamda表達(dá)式(跟run做同樣事情的還有with, let)
run一般用于初始化和返回值的計(jì)算
fun testRun() {
studyOne.run {
println("karl")
}
}
// 輸出結(jié)果
//karl
另一個(gè)run 非擴(kuò)展性功能run
@kotlin.internal.InlineOnly
public inline fun <R> run(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
執(zhí)行閉包们衙,返回閉包結(jié)果
fun main(args: Array<String>) {
val studyOne = run {
StudyOne()
}
println(studyOne)
}
//運(yùn)行結(jié)果
//StudyOne@7a81197d
參考文檔: run
also
/**
* Calls the specified function [block] with `this` value as its argument and returns `this` value.
*
* For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#also).
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block(this)
return this
}
接受一個(gè)對象,返回一個(gè)對象本身,一般使用于不影響對象本身做某些事
fun main(args: Array<String>) {
val numbers = mutableListOf("one", "two", "three")
val alsoTest = numbers.also {
println("kotlin真好用")
println("c: $it")
}
.add("four")
println("c: $numbers")
}
//運(yùn)行結(jié)果
//kotlin真好用
//c: [one, two, three]
//c: [one, two, three, four]
參考文檔: also
with
/**
* Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
*
* For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#with).
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return receiver.block()
}
非擴(kuò)展功能,接受一個(gè)對象碱呼,返回lamda表達(dá)式結(jié)果蒙挑,可以理解為 對這個(gè)對象執(zhí)行以下操作
fun main(args: Array<String>) {
val numbers = mutableListOf("one", "two", "three")
with(numbers) {
this.add("karl")
this.add("帥的起飛")
}
println("numbers: $numbers")
}
//運(yùn)行結(jié)果
//numbers: [one, two, three, karl, 帥的起飛]
參考文檔: with
let
/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*
* For detailed usage information see the documentation for [scope functions](https://kotlinlang.org/docs/reference/scope-functions.html#let).
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
接受一個(gè)對象,返回lamda表達(dá)式結(jié)果,或者指定return
fun main(args: Array<String>) {
val numbers = mutableListOf("one", "two", "three")
fun testLet(): Int {
numbers.let {
return if (Random().nextBoolean()) {
it.add("five")
println(it)
1
} else {
it.add("six")
println(it)
2
}
}
}
}
?.let 對象不為空時(shí)執(zhí)行
參考文檔: let
taskIf
滿足block中條件愚臀,則返回當(dāng)前值忆蚀,否則返回null,block的返回值Boolean類型
takeUnless
不滿足block中條件姑裂,則返回當(dāng)前值馋袜,否則返回null,block的返回值Boolean類型
總結(jié)
執(zhí)行非空對象lamda: ?.let
判斷局部范圍變化: let
對象配置: apply
對象配置和計(jì)算處理: run
運(yùn)行需要的lamda表達(dá)式: 非擴(kuò)展功能 run
附加: also
對象分組調(diào)用: with