-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extensions for Kotlin Flow? #78
Comments
This is an interesting idea that I'd accept a PR for. The naming needs work, not a fan of Given we already have the dependency on kotlin coroutines I presume it could just live in the coroutines submodule? Or does it live in a separate dependency? |
In my opinion the snippets that I posted above are not suitable to go into the library as-is. They were just some ideas on what problems could be encountered when using Starting with the common use-cases definitely makes sense to me. I see 2 broad categories
For me the motivation behind writing those helpers was
I think there is scope for some more generification and simplification on the lines of
That's right. Flow is part of the kotlinx.coroutines dependency. |
Maybe @Munzey, author of the |
Im away for the next two weeks but from a quick read i think it looks like it would be a cool addition that would make perfect sense to include in the coroutines module! As for whether we would need an equivalent |
I'm not really sure the best way to achieve this. It could very well be ignorance on my part. Sorry in advance if its missing the scope of this discussion. This level of generics starts to boggle me too much. How do you achieve a I have an Android room table i want to The function i have atm looks like this:
This isn't valid and won't compile. Since I'm returning Is this a misuse/ understanding on my part or maybe a valid problem? If its valid potentially some further functions with flow and result could assist. |
@Gregory991 Your problem is that .andThen{ id ->
vehicleDao.flowOnUserVehicle(id).map { Ok(it) } // Wraps each emission of Flow<Vehicle> in an Ok thus yielding a Flow<Result<Vehicle, Unit>>
} This might still not compile though. You only fixed the // Disclaimer: This is pseudo-code and i haven't actually tried this in an IDE
suspend fun getUserVehicle(): Flow<Result<Vehicle, Unit>>{
return when(val id = userRepo.getUserVehicle()) {
is Ok -> vehicleDao.flowOnUserVehicle(id).map { Ok(it) } //Flow<Ok<Vehicle<>
is Err -> flowOf(id) //Flow<Err<Unit>> since id is an Err
}
} There might be better ways to do this mapping than using the when clause.
|
@michaelbull I wonder if there is scope for something like this to address @Gregory991's usecase fun <V, E, R> Result<V, E>.toFlow(flowProducer: (V) -> Flow<R>): Flow<Result<R, E> {
when(this) {
is Ok -> flowProducer(this.value).map { Ok(it) }
is Err -> flowOf(this)
}
}
// Usage:
suspend fun getUserVehicle(): Flow<Result<Vehicle, Unit>>{
userRepo.getUserVehicle().toFlow { id ->
vehicleDao.flowOnUserVehcile(id)
}
} |
Have you considered providing extensions for Kotlin Flows, specifically,
Flow<Result<V, E>>
? I'm making heavy use of these "flows-of-result" and I have some helpers of my own. However, I feel that it would be much nicer to havebinding
-style utilities that know aboutFlow
. Here are some examples of helpers that we have in our project (admittedly, some of these can have better names!):Some variants of Flow's
combine
operators that know aboutFlow<Result<V, E>>
Variant of
andThen
that can be applied to aFlow<Result<V, E>>
Variant of
flatMapLatest
that makes it simpler to work with Result<V, E>As you can see, these utilities work, but they can be improved a lot. Specifically, they can benefit from the equivalent of
binding {}
(perhaps something likeflowBinding {}
)The text was updated successfully, but these errors were encountered: