sindrenm
04/03/2024, 7:35 PMonLeft {}
when working with the Raise DSL?
Given the following using when dealing with an API that exposes `Either`s:
fun getIntFromDb(): Either<DbError, Int> = 0.right()
One could “forward” the Either
and also intercept `Left`s for logging or whatever:
fun getInt(): Either<DbError, Int> =
getIntFromDb()
.onLeft { log(it.message) }
However, if the API uses the Raise
DSL, it feels a little more weird:
fun Raise<DbError>.getIntFromDb(): Int = 0
fun Raise<DbError>.getInt(): Int =
withError({
log(it.message)
it
}) { getIntFromDb() }
While writing this, I realized also
could clear some things up, but I'm still curious if this is the Correct™ approach:
fun Raise<DbError>.getInt(): Int =
withError({ it.also { log(it.message) } }) { getIntFromDb() }
sindrenm
04/03/2024, 7:47 PMfun Raise<DbError>.getInt(): Int =
either { getIntFromDb() }
.onLeft { log(it.message) }
.bind()
Feel like I should've thought about that before asking. rubber duckYoussef Shoaib [MOD]
04/03/2024, 10:42 PMonError
I guess. The only API I know that does something similar is traced
, which is useful if you want to log the stack trace as well.Alejandro Serrano.Mena
04/04/2024, 7:31 AMit.also { ... }
.
Feel free to open a discussion in the repo; if more people have this use case, we're happy to explore ways to include it in Arrow 2.0 🙂sindrenm
04/04/2024, 7:35 AMRaise
and Either
using either {}
and bind()
, tbh., but if you think it's worth it, I'd be happy to start a discussion. 👍Wesley Hartford
04/05/2024, 4:17 PMfun Raise<DbError>.getInt(): Int =
either { getIntFromDb() }
.onLeft { log(it.message }
.bind()
Wesley Hartford
04/05/2024, 4:18 PMsindrenm
04/09/2024, 4:41 AM