https://kotlinlang.org logo
#announcements
Title
# announcements
j

Joan Colmenero

10/24/2019, 6:11 PM
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

karelpeeters

10/24/2019, 8:01 PM
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

Joan Colmenero

10/24/2019, 8:22 PM
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

karelpeeters

10/24/2019, 8:23 PM
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

Joan Colmenero

10/24/2019, 8:35 PM
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

karelpeeters

10/24/2019, 8:51 PM
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

Joan Colmenero

10/24/2019, 9:36 PM
Like my WrapperResponse<T>
right?
k

karelpeeters

10/24/2019, 9:37 PM
Heh yeah basically, we're running in circles 🙂
j

Joan Colmenero

10/24/2019, 9:40 PM
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