Joan Colmenero
10/24/2019, 6:11 PMResultWrapper<out T>
but I'm curious now how to implement Either
on the execute method.
I found a way like
data class Success<out T : Any>(val value: T) : ResultWrapper<T>()
karelpeeters
10/24/2019, 8:01 PMEither<Nothing, R>
which is a subtype of any Either<T, R>
.Joan Colmenero
10/24/2019, 8:22 PMinline fun <reified T : Any> execute(requestFunc: () -> T): ResultWrapper<T> =
try {
ResultWrapper.Success(requestFunc.invoke())
} catch (exception: Exception) {
when (exception) {
is UnauthorizedException -> ResultWrapper.Unauthorized(exception)
is NetworkException -> ResultWrapper.Network(exception)
is BadRequestException -> ResultWrapper.BadRequest(exception)
is NotFoundException -> ResultWrapper.NotFound(exception)
else -> ResultWrapper.Error(exception)
}
}
But with my new sealed class Eitherkarelpeeters
10/24/2019, 8:23 PMException
?karelpeeters
10/24/2019, 8:24 PMEither
where one of the cases is an error often has a special type Try
instead)Joan Colmenero
10/24/2019, 8:35 PMJoan Colmenero
10/24/2019, 8:35 PMJoan Colmenero
10/24/2019, 8:35 PMkarelpeeters
10/24/2019, 8:51 PMsealed class Either<out L, out R> {
class Left<out L>(val left: L): Either<L, Nothing>()
class Right<out R>(val right: R): Either<Nothing, R>()
}
inline fun <reified T : Any> execute(requestFunc: () -> T): Either<T, Exception> =
try {
Either.Left(requestFunc.invoke())
} catch (exception: Exception) {
Either.Right(exception)
}
karelpeeters
10/24/2019, 8:52 PMEither<T, Exception>
Try<T>
instead, it's often either a typedef or a separate class.Joan Colmenero
10/24/2019, 9:36 PMJoan Colmenero
10/24/2019, 9:36 PMkarelpeeters
10/24/2019, 9:37 PMJoan Colmenero
10/24/2019, 9:40 PMJoan Colmenero
10/24/2019, 9:40 PMJoan Colmenero
10/24/2019, 9:40 PM