Lukasz Kalnik
02/05/2024, 2:22 PMLeft
to a Right
? Like a flatMapLeft()
.Lukasz Kalnik
02/05/2024, 2:23 PMapi.getUser().fold(
ifLeft = { error ->
if (error.code == 404) EmptyState.right() else ErrorState(error).left()
},
ifRight = { it.right() }
)
Alejandro Serrano.Mena
02/05/2024, 2:33 PMrecover
Youssef Shoaib [MOD]
02/05/2024, 2:33 PMapi.getUser().recover { error ->
if (error.code == 404) EmptyState else raise(ErrorState(error))
}
Lukasz Kalnik
02/05/2024, 2:34 PMeither {}
builder?Alejandro Serrano.Mena
02/05/2024, 2:36 PMval x = recover({ api.getUser().bind() }) {
when (it.code) {
404 -> EmptyState
else -> raise(EmptyState(error))
}
}
Lukasz Kalnik
02/05/2024, 2:37 PMRaise
DSL otherwise?Alejandro Serrano.Mena
02/05/2024, 2:38 PMYoussef Shoaib [MOD]
02/05/2024, 2:39 PMeither {
withError({ error: ErrorType -> if (error.code == 404) return@either EmptyState else ErrorState(error) }) {
api.getUser().bind()
}
}
Youssef Shoaib [MOD]
02/05/2024, 2:42 PMval x = either {
val error = attempt { return@either api.getUsers.bind() }
if (error.code == 404) EmptyState else raise(ErrorState(error))
}
It makes the code less nested, but it's quite imperative, especially with the early-return, and hence some people will dislike it.
You can just replace attempt
with merge
if you want to try this today. There's a minor difference in type safety (attempt forces you to early-return, merge doesn't).Lukasz Kalnik
02/05/2024, 2:45 PMeither {}
block this could be actually a clearer syntax.Youssef Shoaib [MOD]
02/05/2024, 2:46 PMattempt
, I'd love it if you replied with them here or on the PR!