The run () function

The run() function is defined as follows:

@kotlin.internal.InlineOnly
public inline fun <R> run(block: () -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block()
}
Copy the code

Let’s focus on the last line of code, block(), which calls the passed block argument, typically a Lambda block. The following is an example of the test code:

fun myFun(a): String {
    println("Execute myFun")
    return "This is the return value of myFun."
}

fun main(a) {

    myFun() Call the function directly on the line of code

    run({ myFun() }) // Call myFun with run()

    run { myFun() } // The parenthesis "()" of the run() function can be omitted

    run { println("A")}// println("A")
}
Copy the code

The apply () function

The apply() function is defined as follows:

@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T. () - >Unit): T {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block()
    return this
}
Copy the code

Again, we focus on the last two lines of code, which call the block() function and then return the current caller object, this. After executing the block() code block logic, return the current caller object again. The following is an example of the test code:

fun testApply(a) {
    //
    val list = mutableListOf<String>()
    list.add("A")
    list.add("B")
    list.add("C")
    println("Normal write list =$list")

    // use the apply() function
    val a = ArrayList<String>().apply {
        add("A")
        add("B")
        add("C")
        println("Use apply to write this +$this")
    }

    println(a)

    / / equivalent to the
    a.let { println(it) }
}
Copy the code

Let () function

Let () is defined as follows:

@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) - >R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block(this)}Copy the code

Again, we’ll focus on the last line of code, block(this), which means passing in the current calling object as an argument to the block() code block. The following is an example of the test code:

fun myFun(a): String {
    println("Execute myFun")
    return "This is the return value of myFun."
}

fun testLetFun(a) {
    "ABC".let {
        println(it)
    }

    myFun().let {
        // after executing myFun, pass the return value to let()
        println(it)
    }

}

fun main(a) {
    testLetFun()
}
// ABC
// Execute myFun
// This is the return value of myFun
Copy the code

Braking () function

The also() function is defined as follows:

@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
}
Copy the code

Block (this) is called, similar to let() logic, but returns this, the current caller. The following is an example of the test code:

fun testAlsoFun(a){
    val a = "ABC".also {
        println(it) // output: ABC
    }

    println(a) // output: ABC
    
    a.let { 
        println(it) // output: ABC}}Copy the code

With the function

The with function is defined as follows:

@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T. () - >R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return receiver.block()
}
Copy the code

We see that the with() function passes in a receiver object, receiver, and then uses that object to call the passed Lambda block receiver.block(). The test code is as follows:

fun testWithFun(a) {
    //
    val list = mutableListOf<String>()
    list.add("A")
    list.add("B")
    list.add("C")
    println("The conventional way to write it is list equals$list")

    // Use the with() function
    with(ArrayList<String>()) {
        add("A")
        add("B")
        add("C")
        println("Use the with function to write this =$this")
    }.let {
        println(it)
    }
}

fun main(a) {
    testWithFun()
}

List = [A, B, C]
// This = [A, B, C]
// kotlin.Unit
Copy the code