为什么流的组合运算符与数组一起使用?
Why does the combine operator of flow works with arrays?
为什么流的组合运算符适用于数组(而不是列表或迭代器)?
来自源代码:
public inline fun <reified T, R> combine(
flows: Iterable<Flow<T>>,
crossinline transform: suspend (Array<T>) -> R
): Flow<R> {
val flowArray = flows.toList().toTypedArray()
return flow {
combineInternal(
flowArray,
arrayFactory = { arrayOfNulls(flowArray.size) },
transform = { emit(transform(it)) })
}
}
估计是为了方便,因为大多数combine...
方法都是使用vararg flows: Flow<T>
参数调用combineInternal()
函数,直接把flows
传给combineInternal()
方法而不将其转换为另一种数据结构。接受 vararg flows: Flow<T>
参数的方法示例:
public inline fun <reified T, R> combine(
vararg flows: Flow<T>,
crossinline transform: suspend (Array<T>) -> R
): Flow<R> = flow {
combineInternal(flows, { arrayOfNulls(flows.size) }, { emit(transform(it)) })
}
为什么流的组合运算符适用于数组(而不是列表或迭代器)?
来自源代码:
public inline fun <reified T, R> combine(
flows: Iterable<Flow<T>>,
crossinline transform: suspend (Array<T>) -> R
): Flow<R> {
val flowArray = flows.toList().toTypedArray()
return flow {
combineInternal(
flowArray,
arrayFactory = { arrayOfNulls(flowArray.size) },
transform = { emit(transform(it)) })
}
}
估计是为了方便,因为大多数combine...
方法都是使用vararg flows: Flow<T>
参数调用combineInternal()
函数,直接把flows
传给combineInternal()
方法而不将其转换为另一种数据结构。接受 vararg flows: Flow<T>
参数的方法示例:
public inline fun <reified T, R> combine(
vararg flows: Flow<T>,
crossinline transform: suspend (Array<T>) -> R
): Flow<R> = flow {
combineInternal(flows, { arrayOfNulls(flows.size) }, { emit(transform(it)) })
}