kotlin小ネタ:expression の値が null の場合のみ expression の値を変えずに処理を行う例

// オリジナル
val currentValue = nextValue()
if (currentValue == null) finish()

// 等価コード1
val currentValue = nextValue().apply { if (this == null) finish() }

// 等価コード2
val currentValue = nextValue() ?: { finish(); null }()

elvis operator を使う場合に expression が null を返すのが面倒なら
こういうのを作っちゃっう方法もありかも。

/**
 * Calls the specified function [block] with `this` value as its receiver only if `this` is null and returns `this` value.
 */
inline fun <T> T.applyIfNull(block: T.() -> Unit): T {
    if (this == null) block(); return this
}
// 等価コード3
val currentValue = nextValue().applyIfNull { finish() }