Youssef Shoaib [MOD]
05/22/2024, 3:06 PMstreetsofboston
05/22/2024, 3:52 PMYoussef Shoaib [MOD]
05/22/2024, 3:59 PMflowComprehension {
val value1 = flow1.collectHere()
val value2 = flow2.collectHere()
...
value1 + value2
} // Returns a Flow<Int> as a result
This syntax wouldn't be only for flows though, it would work for any datatype (technically it's for Monads but that's an often-confusing term). Similar code can be written for a list comprehension, a sequence comprehension, etc. One can also use different types with it:
flowComprehension {
listComprehension {
// One can collect flows or lists here freely as they wish. Probably. It'd depend on a nice-enough implementation
}
}
Look, it's similar to why have suspend functions vs just using callbacks. Any code written with suspend can also be written with callbacks. Similarly here, any code with multishot continuations can just be written with callbacks. The advantage of writing it in direct style (other than aesthetics and ease of understanding) is also that the compiler can perform certain optimizations automatically.bobko
07/10/2024, 6:01 PMtry-catch-finally
. Contrary, in (1), suspend functions combined with `for`/`try-catch` was a selling point (again, because programmers don't need to remember names of the operators)Youssef Shoaib [MOD]
07/10/2024, 6:23 PMstreetsofboston
07/10/2024, 6:32 PMflow {
val value1 = flow1.first()
val value2 = flow2.first()
emit(value1 + value2)
} // Returns a Flow<Int> as a result
Youssef Shoaib [MOD]
07/10/2024, 6:43 PMflatMapConcat
streetsofboston
07/10/2024, 8:06 PM