ifEmpty所在位置是kotlin.sequences.ifEmpty,其相关用法介绍如下。

用法:

fun <T> Sequence<T>.ifEmpty(
    defaultValue: () -> Sequence<T>
): Sequence<T>

返回一个序列,该序列遍历此序列中的元素,或者如果此序列为空,则返回 defaultValue 函数返回的序列中的元素。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val empty = emptySequence<Int>()

val emptyOrDefault = empty.ifEmpty { sequenceOf("default") }
println(emptyOrDefault.toList()) // [default]

val nonEmpty = sequenceOf("value")

val nonEmptyOrDefault = nonEmpty.ifEmpty { sequenceOf("default") }
println(nonEmptyOrDefault.toList()) // [value]
//sampleEnd
}

输出:

[default]
[value]