Android-CleanArchitecture-Kotlin
Android-CleanArchitecture-Kotlin copied to clipboard
How to consume the result object of Either in Use Case Layer?
So there is a repository layer and a user case layer. My use case calls the repository method (which returns either left or right and returns the User object as output when right.

Hi. Any ideas?
Not sure if this is correct but I can get it through result.either call and provide left or right callbacks, in which in the right callback, I can receive the User param.
Have you tried .fold()?
Have you tried
.fold()?
Nope, that method is not available in either.
You could use .flatMap(). it gives access to the Right object.
result.flatMap() { it -> repository.saveUser(it) }
Hmm according to Arrow's Either implementation, there should be a fold method.
inline fun <R> fold(ifLeft: (LEFT) -> R, ifRight: (RIGHT) -> R): R = when (this) {
is Right -> ifRight(right)
is Left -> ifLeft(left)
}
Hmm according to Arrow's Either implementation, there should be a
foldmethod.
Yes, Arrow's implementation does have fold method. But this project has a custom Either, it doesn't have all of Arrow methods. It only has flatmap() and map().
Well it's kinda neutered if it cannot fold. That's kinda the whole point of either 😄
You could use
.flatMap(). it gives access to the Right object.
result.flatMap() { it -> repository.saveUser(it) }
Thanks for this but doesn't flatmap transform data (instead of accessing object only) ? what is the impact of the flatmap operation to the original result variable?
Also upon checking the code, what is the difference between map and flat map?
flatMap expects some kind of Either<L, R> while map in case of Either most likely means mapRight which expects some kind of R.