Joost Klitsie
06/28/2024, 10:28 AMfun fetchDataForLoggedInUser(): Result<String> = fetchLoggedInUserUseCase.run()
.flatMap { user -> fetchDataForUserUseCase.run(user) }
I of course made my own extension function (in the comment), but would you people agree that it would be a nice thing to add to the standard library as well?Joost Klitsie
06/28/2024, 10:28 AMinline fun <T, R> Result<T>.flatMap(
transform: (T) -> Result<R>,
): Result<R> {
contract {
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}
return transform(getOrElse { return failure(it) })
}
CLOVIS
06/28/2024, 12:48 PMResult
contains a CancellationException, this function will execute transform
, whereas it shouldn't since the coroutine has been cancelled. Instead, it should call coroutineContext.ensureActive
before executing transform
, but that requires making the entire function suspend
.Joost Klitsie
06/28/2024, 12:52 PMCLOVIS
06/28/2024, 12:53 PMrunCatching
Joost Klitsie
06/28/2024, 12:53 PMCLOVIS
06/28/2024, 12:54 PMJoost Klitsie
06/28/2024, 1:24 PMCLOVIS
06/28/2024, 1:24 PMrunCatching
should just be moved back to be an internal of kotlinx.coroutines, where it belongs 😅CLOVIS
06/28/2024, 1:25 PMThrowable
, which is always an anti-pattern. Not a single application, and even more in the domain layer, has an appropriate way to recover from a StackOverflowError
. And that's without counting on OutOfMemoryError: metaspace
, there is nothing you can do to recover that.