Skip to content

Instantly share code, notes, and snippets.

@LukaKordic
Last active August 13, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukaKordic/555c9187aa1adcf62a4c7417eb73b117 to your computer and use it in GitHub Desktop.
Save LukaKordic/555c9187aa1adcf62a4c7417eb73b117 to your computer and use it in GitHub Desktop.
sealed class Result<out T : Any>
data class Success<out T : Any>(val data: T) : Result<T>()
data class Failure(val httpError: HttpError) : Result<Nothing>()
class HttpError(val throwable: Throwable, val errorCode: Int = 0)
inline fun <T : Any> Result<T>.onSuccess(action: (T) -> Unit): Result<T> {
if (this is Success) action(data)
return this
}
inline fun <T : Any> Result<T>.onFailure(action: (HttpError) -> Unit) {
if (this is Failure) action(httpError)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment