I've found a way to keep using `ResultWrapper<o...
# announcements
j
I've found a way to keep using
ResultWrapper<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>()
k
I'm not 100% sure what your question is but you might want to use
Either<Nothing, R>
which is a subtype of any
Either<T, R>
.
j
I want to change this :
Copy code
inline 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 Either
k
And what do you want the error type to be? Just
Exception
?
(FYI:
Either
where one of the cases is an error often has a special type
Try
instead)
j
Don't get you what you said in the last answer
Yes, or just a sealed class with all the errors
All extends Exception though
k
Simplified code:
Copy code
sealed 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)
        }
What I meant with my FYI is that people/libraries usually call
Either<T, Exception>
Try<T>
instead, it's often either a typedef or a separate class.
j
Like my WrapperResponse<T>
right?
k
Heh yeah basically, we're running in circles 🙂
j
But the thing is that I can not do like a fold
having the exception and the T
I have to do it with a when