本章节主要目的:通过表格显示let、with、run、appply、also区别
主要从两个维度来区分:函数内使用对象、返回值
| 函数名 | inline结构 | 函数内使用对象 | 返回值 |
|---|---|---|---|
| let | fun <T, R> T.let(block: (T) -> R): R { block(this) } | it | return或者最后一行 |
| with | fun <T, R> with(receiver: T, block: T.() -> R): R { receiver.block() } | T类型对象receiver | return 或者最后一行 |
| run | fun <T, R> T.run(block: T.() -> R): R { block() } | this | return 或者最后一行 |
| apply | fun T.apply(block: T.() -> Unit): T {block();return this} | this | 对象本身 或者最后一行 |
| also | fun T.also(block: (T) -> Unit): T { block(this); return this } | it | 对象本身 或者最后一行 |
如果想了解详细的使用场景,推荐文章:blog.csdn.net/u013064109/…