abbic
10/23/2023, 9:16 AMFlow<String>
becomes Flow<Result<String>>
for example)?
are there any immediate complications from this approach? Example implementation coming soon in commentsfun <T : Any> Flow<T>.asResultFlow(): Flow<Result<T>> = flow {
catch {
emit(Result.failure(it))
}.collect {
emit(Result.success(it))
}
}
Stylianos Gakis
10/23/2023, 2:40 PMFlow<Either<L, R>>
and it’s super convenient.
Don’t see why you wouldn’t be able to just do that.
Just as long as you don’t also catch CancellationExceptions.abbic
10/23/2023, 2:43 PMStylianos Gakis
10/23/2023, 2:45 PMCancellationException
, you need to re-throw it in that case.abbic
10/23/2023, 2:46 PMStylianos Gakis
10/23/2023, 2:46 PMinternal suspend fun <T> Flow<T>.catch(
implementation, you will see they also re-throw if the catch a CancellationException. So if you only use that to catch then you’re fine alreadyabbic
10/23/2023, 2:46 PMStylianos Gakis
10/23/2023, 2:47 PMrunCatching {}
does catch CancellationException too, and does not rethrow it itself, you need to do that manually with itviewModelScope
for example, if you use that
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:lifecycl[…]java/androidx/lifecycle/ViewModel.kt;l=35-45?q=viewModelScope
It will throw the exception to the Scope, but since it uses SupervisorJob
it will not cancel the scope, nor the other jobs working on it.abbic
10/23/2023, 2:49 PMStylianos Gakis
10/23/2023, 2:49 PM