80 lines
3.6 KiB
Markdown
80 lines
3.6 KiB
Markdown
`run`, `let`, `with`, `also` 和 `apply` 這幾個都是可以搭配 object 使用的函數,它們之間的差異不大,主要是讓程式在語意上更加流暢。
|
||
以下解釋各個的差別。
|
||
|
||
## 歸納
|
||
| | 變數名稱 | 自訂變數名稱 | 回傳 |
|
||
|:--------------:|:----------:|:------------:|:--------:|
|
||
| `run()` | | | 最後一行 |
|
||
| `with()` | `this` | No | 最後一行 |
|
||
| `object.run` | `this` | No | 最後一行 |
|
||
| `object.let` | `it` | Yes | 最後一行 |
|
||
| `object.also` | `it` | Yes | `this` |
|
||
| `object.apply` | `this` | No | `this` |
|
||
|
||
## run
|
||
`run` 後面的區塊會**回傳「最後一行」**,所以可以進行「串接」。如下:
|
||
```kotlin
|
||
run {
|
||
val telephone = Telephone()
|
||
telephone.whoCallMe = "English"
|
||
telephone // <-- telephone 被帶到下一個 Chain
|
||
}.callMe("Softest part of heart") // <-- 這裡可以執行 `Telephone` Class 的方法
|
||
```
|
||
|
||
## object.run
|
||
`object.run` 跟[[#run]]是一樣的,也是**回傳「最後一行」**,只是 `object.run` 是讓 object 呼叫的,而且 lambda scope 中的物件會變成 `this`,`this` 可以省略,但是不可以自訂變數名稱,如下:
|
||
```kotlin
|
||
val anObject = MyObject()
|
||
anObject.run {
|
||
this.doSomething() // this就是anObject,this可以省略
|
||
}
|
||
```
|
||
|
||
## with
|
||
`with(T)` 之中的傳入值可以以 `this` (稱作 identifier) 在 scope 中取用,不用打出 `this`也沒關係。雖然, `with` 也會**將最後一行回傳**。但是因為 `with` 沒有先判斷物件是否為 `null`,所以 scope 中還是要用`?`來判斷是否 `null`,如下:
|
||
```kotlin
|
||
val anObject = makeMyObject() // anObject有可能是null
|
||
with(anObject) {
|
||
this?.doSomething() // this就是anObject,this可能是null
|
||
}
|
||
```
|
||
|
||
## object.let
|
||
`let` 的 scope 裡,物件是 `it`,可以自訂變數名稱,一樣會**回傳最後一行**。如下:
|
||
```kotlin
|
||
val anObject = makeMyObject()
|
||
anObject?.let { // anObject有可能是null,所以要先用?判斷
|
||
it.doSomething() // it就是anObject,在scope內一定不是null
|
||
}
|
||
```
|
||
|
||
## object.also
|
||
`also` 的 scope 裡,物件是 `it`,可以自訂變數名稱,但是不是回傳最後一行,是**回傳自己**(也就是 `it`)。如下:
|
||
```kotlin
|
||
val anObject = makeMyObject()
|
||
anObject?.also { // anObject有可能是null,所以要先用?判斷
|
||
it.doSomething() // it就是anObject,在scope內一定不是null
|
||
}.also {
|
||
it.doSomething2() // it就是anObject
|
||
}.also {
|
||
it.doSomething3() // it就是anObject
|
||
}
|
||
```
|
||
|
||
## object.apply
|
||
`apply` 的 scope 裡,物件是 `this`,不可以自訂變數名稱,但是不是回傳最後一行,是**回傳自己**(也就是 `this`)。如下:
|
||
```kotlin
|
||
val anObject = makeMyObject()
|
||
anObject?.also { // anObject有可能是null,所以要先用?判斷
|
||
this.doSomething() // this就是anObject,在scope內一定不是null
|
||
}.also {
|
||
this.doSomething2() // this就是anObject
|
||
}.also {
|
||
this.doSomething3() // this就是anObject
|
||
}
|
||
```
|
||
|
||
## 參考
|
||
- [簡介 Kotlin: run, let, with, also 和 apply](https://louis383.medium.com/%E7%B0%A1%E4%BB%8B-kotlin-run-let-with-also-%E5%92%8C-apply-f83860207a0c)
|
||
- [Kotlin 的 scope function: apply, let, run..等等](https://jchu.cc/2018/05/05-kotlin.html)
|
||
- [Kotlin 線上讀書會 筆記 (五) apply、let、run、with、also和takeIf](https://medium.com/evan-android-note/kotlin-%E7%B7%9A%E4%B8%8A%E8%AE%80%E6%9B%B8%E6%9C%83-%E7%AD%86%E8%A8%98-%E4%BA%94-apply-let-run-with-also%E5%92%8Ctakeif-2c09d42b09b5) |