Ifvwm
06/22/2019, 3:32 AMstreetsofboston
06/22/2019, 3:48 AMpoly
returns a value of type E
and the sealed class E
itself does not define the property v
.Ifvwm
06/22/2019, 6:18 AMPavlo Liapota
06/22/2019, 6:36 AMNikky
06/22/2019, 10:02 AMpoly2
streetsofboston
06/22/2019, 12:51 PMEither
or Result
, this may work for you.
sealed class Result<out E, out T> {
data class Success<out T>(val value: T) : Result<Nothing, T>()
data class Failure<out E>(val error: E) : Result<E, Nothing>()
}
fun <E, T, T2> Result<E, T>.map(function: (T) -> (T2)): Result<E, T2> = when (this) {
is Result.Success -> Result.Success(function(value))
is Result.Failure -> this
}
fun <E, E2, T> Result<E, T>.mapError(function: (E) -> (E2)): Result<E2, T> = when (this) {
is Result.Success -> this
is Result.Failure -> Result.Failure(function(error))
}
fun <E, T, R> Result<E, T>.extract(whenFailure: (E) -> R, whenSuccess: (T) -> R): R = when (this) {
is Result.Success -> whenSuccess(value)
is Result.Failure -> whenFailure(error)
}
where E
is the error-type and T
is the value type.
You can create additional extension functions for your own particular use-cases.Ifvwm
06/23/2019, 2:58 AMstreetsofboston
06/23/2019, 1:16 PM