Is it possible to have kotlin Result be exhaustive...
# android
d
Is it possible to have kotlin Result be exhaustive it seems to hide Success and Failure so I want to basically do something like this within a extension function
Copy code
fun <T> Result<T>.toDataState(): DataState<T> {
    onFailure {
        return DataState.failure()
    }.onSuccess {
        return DataState.success(it)
    }
}
But you can not because onFailure and onSuccess is not exhaustive. How have you done this?
c
You can use
Result<T>.fold()
for this
🙏 2
e
also equivalent to
Copy code
return result.map {
    Data.Success(it)
}.getOrElse {
    Data.Failure()
}
which isn't the best choice for this application but you might use something similar if the transform itself can throw and you want to flatten that error (e.g. with
mapCatching
,
recoverCatching
)
🙏 1
m
@David Corrado I have created a object class for this.Please check and let me know it would be work as you expected