Good morning. I have a function that accepts a lam...
# arrow
d
Good morning. I have a function that accepts a lambda that returns
Either<NetworkError, T>
Something like
Copy code
fun <T : Any> MyFun(
    ...
    fetch: suspend () -> Either<NetworkError, T>,
    ...
): Flow<Either<DataError, T>>
Now I have a new use case, as I started supporting a guest mode, so besides
NetworkError
and
T
I need something to represent a case when the user is not logged in, which I don’t want to be part of either of the 2 mentioned. What would you suggest to use instead of
Either<NetworkError, T>
? (
Skipped
= call didn’t happen ) Here are some solutions that I thought of, but dunno which one would be nicer, or if there are other solutions: •
Either<Either<NetworkError, Skipped>, T>
this would be the easier one to achieve/refactor; not sure how nice it would be to nest 2
Either
Either<NetworkResult, T>
Copy code
sealed interface NetworkResult {
  object Skipped : NetworkResult
  data class Error(val networkError: NetworkError)
}
• Replace
Either
with a sealed interface, like following
Copy code
sealed interface NetworkResult<T> {
  object Skipped : NetworkResult<Nothing>
  data class Error<Nothing>(val networkError: NetworkError)
  data class Data(val data: T)
}
The last solution is probably the cleanest, but I’d lose the
Either
operators that I’m currently using, like
tap
,
fold
,
bind
, etc The second one would be my preferred
s
I'd also go with the second option... You could also wrote your computational block (like either {}) for your own NetworkResult type... But going with the second option is simpler
d
Already did that before Arrow was multiplatform, but don't wanna reinvent the wheel again 😃 I'm trying the second option