A bit of a blasphemous question here. I love the
Raise
DSL over the older
either
one, moving from something like
suspend fun findUser(userId: Int): Either<UserNotFound, User> = either {
// success case
anotherFunction(userId).bind()
User(1)
// failure case
UserNotFound(userId).left().bind<User>()
}
to
context(Raise<UserNotFound>)
suspend fun findUser(userId: Int): User {
// success case
anotherFunction(userId)
User(1)
// failure case
raise(UserNotFound(userId))
}
Looking at the the latter it's pretty much the syntax you'd use when using good old fashioned exceptions like
@Throws(UserNotFoundException::class)
suspend fun findUser(userId: Int): User {
// success case
anotherFunction(userId)
return User(1)
// failure case
throw UserNotFoundException(userId)
}
Now at the top level I need to deal with the exception instead of dealing with an Either but using
runCatching
makes that fairly simple. So how is the
Raise
solution better than the throws solution? I need to convince a large team of engineers that the former has advantages so need good arguments but tbh I haven't had any convincing ideas yet. Any suggestions?