Vera van Mondfrans
10/01/2020, 1:19 PMEither
with Flow
when the flow can emit values before coming across something that should cause failure. For example:
fun getNumbersThatAreNotEleven(n: Int): Either<ThatsElevenException, Flow<Int>> =
flow {
for(i in 1..n) {
if (i == 11) {
// sacrilege, what do I do here?
} else {
emit(i)
}
}
}
albertosh
10/01/2020, 2:11 PMflow
is lazily evaluated for each element, so unless you can recognize that something will fail at any point in time, you’d have already emitted the first itemssimon.vergauwen
10/01/2020, 2:13 PMfun getNumbersThatAreNotEleven(n: Int): Flow<Either<ThatsElevenException, Int>>
This would workVera van Mondfrans
10/01/2020, 2:24 PMVera van Mondfrans
10/01/2020, 2:24 PM