Erik Dreyer
06/12/2025, 4:19 PMEither.catch() I find myself wanting to treat it like either {} and use various DSL methods provided by Raise inside the catch() function. Since it doesn't provide that context, is this the correct way?
either {
Either.catch() { ... }.bind()
}Youssef Shoaib [MOD]
06/12/2025, 4:29 PMresult builder does something similar btw, so this might be a good definition for a util:
result(block).fold(::Right, ::Left)simon.vergauwen
06/12/2025, 4:29 PMYoussef Shoaib [MOD]
06/12/2025, 4:31 PMEither.catching or similarly-named builder might be warranted here, implemented in a similar way to result {} (i.e. using fold)simon.vergauwen
06/12/2025, 4:36 PMensure(false) { RuntimeException() } then you’re still paying the penalty of exceptions anyway. So you might as well just throw. If that’s not the case then the last 2 snippets from the linked thread might work nicely for you.Erik Dreyer
06/12/2025, 5:58 PMprivate fun getToken(tokenId: UUID): Either<DataStorageError, Decrypted> = either {
Either.catch {
tokenDao.getToken(tokenId) ?: throw NoSuchElementException("Token not found for tokenId: $tokenId")
}.mapLeft { exception -> handleTokenExceptions(exception)
}.bind()
}
I get what it does, but I can't help feeling there's a less awkward way to express this.
maybe instead of this
tokenDao.getToken(tokenId) ?: throw NoSuchElementException("Token not found for tokenId: $tokenId")
you could do
ensureNotNull(tokenDao.getToken(tokenId)) { NoSuchElementException(...)}
and deal with it at the outer either {} blockErik Dreyer
06/12/2025, 6:00 PMtokenDao.getToken(tokenId) can throw other exceptions, so we still need the Either.catchsimon.vergauwen
06/12/2025, 6:02 PMthrow NoSuchElementException("Token not found for tokenId: $tokenId") if getToken is null, but can getToken also throw?
If not, then it's probably just, tokenDao.getToken(tokenId).right() ?: handleTokenExceptions(?).Erik Dreyer
06/12/2025, 6:02 PMsimon.vergauwen
06/12/2025, 6:02 PMeither { } with a single bind and nothing after it is probably redundant as well. This could also just be:
Either.catch {
tokenDao.getToken(tokenId) ?: throw NoSuchElementException("Token not found for tokenId: $tokenId")
}.mapLeft { exception -> handleTokenExceptions(exception) }Erik Dreyer
06/12/2025, 6:03 PM