终于理解 kotlin 语法糖 Also

92 阅读1分钟

之前不懂 also 使用的实际意义在哪,直到看到 Java 转 Kotlin 时,发现

Java 代码

private void scheduleNext() {
    if ((mActive = mArrayDeque.poll()) != null) {
        threadPoolExecutor.execute(mActive);
    }
}

转换成 Kotlin 代码

private fun scheduleNext() {
    if (mArrayDeque.poll().also { mActive = it } != null) {
        threadPoolExecutor?.execute(mActive)
    }
}

才恍然大悟, 原来是顺带执行 { ... } 的意思