Having this : ``` sealed class ResultWrapper<ou...
# announcements
j
Having this :
Copy code
sealed class ResultWrapper<out T> {
    data class Success<T>(val data: T) : ResultWrapper<T>()
    data class Unauthorized<T>(val exception: UnauthorizedException) : ResultWrapper<T>()
    //More error classes
}
I could not get the T when I was using
ResultWrapper.
I was using like :
Copy code
when(function) {
 is ResultWrapper.Success -> DoSomething
 is ResultWrapper.SomeError -> DoSomething
And I realized that I could not use the value of the return of it, so I found this sealed class
Copy code
sealed class Either<out L, out R> {
    //Failure
    data class Left<out L>(val value: L) : Either<L, Nothing>()
    //Success
    data class Right<out R>(val value: R) : Either<Nothing, R>()
    val isRight get() = this is Right<R>
    val isLeft get() = this is Left<L>
    fun <L> left(a: L) = Left(a)
    fun <R> right(b: R) = Right(b)
}
fun <L, R, T> Either<L, R>.fold(left: (L) -> T, right: (R) -> T): T =
    when (this) {
        is Either.Left  -> left(value)
        is Either.Right -> right(value)
    }
fun <L, R, T> Either<L, R>.flatMap(f: (R) -> Either<L, T>): Either<L, T> =
    fold({ this as Either.Left }, f)
fun <L, R, T> Either<L, R>.map(f: (R) -> T): Either<L, T> =
    flatMap { Either.Right(f(it)) }
And with this I can do it, the thing is I was using 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)
            }
        }
And then trying to move it to
Either
I'm having problems since I have to do something like
reified T,R
and the return is
Either<T,R>
but then trying to do
Either.Right(requestFunc.invoke())
is saying that is expecting a T,R and I'm only returning T. Any ideas how to achieve it?