Kotlin 操作符:run、with、let、also、apply 的差异与选择

Kotlin 操作符:run、with、let、also、apply 的差异与选择

原文:Kotlin 操作符:run、with、let、also、apply 的差异与选择

通过参考原文,通过mermaid画出流程图,没有任何新东西。/笑脸

graph TD
  A(选择标准函数) --> B{"return this" }
  subgraph subB ["需要返回自身(this)"]
        B
        %%noteA["需要返回自身(this)"]
    end
  B -->|NO| C{"T.block()"}
  subgraph subC["需要扩展函数?(空检查,链式调用)"]
        C
        %%noteC[" "]
    end
  C -->|NO| D{"传递this作为参数"}
  D -->|NO| E("使用run()")
  D -->|YES| F("使用with(T)")
  C -->|YES| G{"传递it/this作为参数"}
  G -->|传递this| H("使用T.run()")
  G -->|传递it| I("使用T.let()")
  B -->|YES| J{"block(it or this)"}
    subgraph subJ["传递it/this作为参数"]
        J
       %% noteJ[" "]
    end
  J-->|传递this| K("使用T.apply()")
  J-->|传递it| L("使用T.also()")
  style A font-size:14px
  style B extends A,font-size:10px