Kotlin 操做符:run、with、let、also、apply、takeIf、takeUnless、repeat

概述

分析Kotlin的 Standard.kt 代碼,主要分爲三部分:網絡

  1. run、with、let、also、apply 的比較
  2. takeIf、takeUnless、repeat 的使用
  3. 異常類的使用

1、run、with、let、also、apply 的比較

全部的總結都源自於代碼,因此最終仍是要回到代碼中找到答案。app

示例

1. run()

/** * Calls the specified function [block] and returns its result. */
@kotlin.internal.InlineOnly
public inline fun <R> run(block: () -> R): R = block()
複製代碼

兩種寫法:T.run({...})T.run {},第二種寫法是第一種的變種,當方法的最後一個參數是lambda表達式時,能夠將表達式移出;less

class Main {

    fun test(){
	    // 第一種寫法
        val run1 = run({
            Log.d("Main", "我是內部的Run")
            "Run1"
        })
        Log.d("Main", "我是內部的Result=$run1") //我是內部的Result=Run1

		//第二種寫法
        val run2 = run {
            Log.d("Main", "我是外部的Run")
            "Run2"
        }
        Log.d("Main", "我是外部的Result=$run2" ) ////我是外部的Result=Run2
    }
}
複製代碼

2. T.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()
複製代碼
class Main {
    fun test(){
	val run = "ABCDEF".run {
            substring(2) //能夠在內部直接調用String的方法
        }
        Log.d("Main", "T.run()的值 = $run" ) //T.run()的值 = CDEF
    }
}
複製代碼

3. 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()
複製代碼
class Main {
    fun test(){
	val with = with("ABCDEF") {
            substring(2)
        }
        Log.d("Main", "with()的值 = $with" ) //with()的值 = CDEF
    }
}
複製代碼

4. 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 }
複製代碼
class Main {
    fun test(){
	val apply = "ABCDEF".apply {
            it.substring(2)
        }
        Log.d("Main", "T.apply()的值 = $apply" )//T.apply()的值 = ABCDEF,值沒有被改變
    }
}
複製代碼

5. 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 }
複製代碼
class Main {
    fun test(){
	val also = "ABCDEF".also {
            it.substring(2)
        }
        Log.d("Main", "T.also()的值 = $also" )//T.also()的值 = ABCDEF,值沒有被改變
    }
}
複製代碼

6. 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)
複製代碼
class Main {
    fun test(){
	val let = "ABCDEF".let {
            it.substring(2) //這裏須要使用it
        }
        Log.d("Main", "T.let()的值 = $let" ) //T.let()的值 = CDEF
    }
}
複製代碼

2. 結論

圖:(圖片來源於網絡) ui

這裏寫圖片描述

源碼:this

public inline fun <R> run(block: () -> R): R = block()
public inline fun <T, R> T.run(block: T.() -> R): R = block()
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
複製代碼

從三個維度去分析:spa

  1. 該操做符是被某個類的對象調用,仍是直接調用(即:run() 仍是 T.run() )。
  2. 對象T是否被當作參數(即:T.also()T.let())。
  3. 返回值是不是return this(即:T.apply()T.also())。
操做符 接收者(this) 參數(it) 返回值(result) 典型應用場景
run ( block: () -> R ) : R 當前類 / 類R(最後一行)
T.run ( block: T.() -> R ) : R 類T / 類R(最後一行)
with ( receiver: T, block: T.() -> R ) : R 類T / 類R(最後一行)
T.apply ( block: T.() -> Unit ) : T 類T / 類T
T.also ( block: (T) -> Unit ) : T 當前類 類T 類T
T.let ( block: (T) -> R ) : R 當前類 類T 類R(最後一行)

分析:

  1. run()T.run() 比較:code

    • T.run() 的參數T.()表示在其block中能夠直接使用T的全部public方法,而run() 卻不行;
  2. T.apply()、T.also() 和 其餘幾個比較:cdn

    • 返回值都是return this,因此返回的都是以前的對象T ;
    • 因爲 T.also() 方法體中有 block(this),將 this 做爲參數傳入,因此在Lambda類型的block內部就不能再用this 獲取當前的傳入參數T,而要使用it 獲取T,而this 實際表明的是當前所處的類;
  3. T.also()、T.let() 和 其餘幾個比較:對象

    • 這兩個方法都將T做爲參數傳入,且方法體中都將 this 傳入 block(this),因此T必須都要用it來獲取;

2、takeIf、takeUnless、repeat 的使用

源碼

/** * Returns `this` value if it satisfies the given [predicate] or `null`, if it doesn't. * * 根據當前predicate的返回值判斷:若爲空,則返回null,若不爲空,則返回原值 * 根據上面的總結,在predicate中獲取傳入的參數T時要使用it,而不是this; */
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null

/** * Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does. */
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
複製代碼

示例

class Main {

    fun test(){
        val takeif = "ABC".takeIf {
            Log.d("Main", "this = $this") //this = com.example.kotlindemo.Main@f17fc13
            Log.d("Main", "it = $it") //it = ABC
            true //若是是false,則返回null
        }
        Log.d("Main", "我是takeif=$takeif" ) //我是takeif=ABC
    }
}
複製代碼
/** * Executes the given function [action] specified number of [times]. * * A zero-based index of current iteration is passed as a parameter to [action]. */
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
    for (index in 0..times - 1) {
        action(index)
    }
}
複製代碼
class Main {
    fun test(){
        repeat(10) {
            Log.d("Main",  "this = $this") //this = com.example.kotlindemo.Main@f17fc13
            Log.d("Main",  "$it")
        }
    }
}
複製代碼

輸出的值:blog

com.example.kotlindemo D/Main: 0 com.example.kotlindemo D/Main: 1 com.example.kotlindemo D/Main: 2 com.example.kotlindemo D/Main: 3 com.example.kotlindemo D/Main: 4 com.example.kotlindemo D/Main: 5 com.example.kotlindemo D/Main: 6 com.example.kotlindemo D/Main: 7 com.example.kotlindemo D/Main: 8 com.example.kotlindemo D/Main: 9


3、異常類的使用

源碼

public class NotImplementedError(message: String = "An operation is not implemented.") : Error(message)
public inline fun TODO(): Nothing = throw NotImplementedError()
public inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")
複製代碼

示例

//Kotlin代碼
class Command {
	fun execute(){
		TODO() //拋出異常
	}
}

class Test {
	val command = Command()
	command.execute()// 當執行到這行代碼時,就會拋出異常NotImplementedError
}
複製代碼
相關文章
相關標籤/搜索