Skip to content

Instantly share code, notes, and snippets.

@MaxMichel2
Created October 2, 2024 15:52
Show Gist options
  • Save MaxMichel2/b2ce86c0208b1c1cecb9c28be4f1f1e2 to your computer and use it in GitHub Desktop.
Save MaxMichel2/b2ce86c0208b1c1cecb9c28be4f1f1e2 to your computer and use it in GitHub Desktop.
sealed class Result<out D, out E> {
data class Success<out D>(val data: D) : Result<D, Nothing>()
data class Error(val error: AppError) : Result<Nothing, Nothing>()
data class BusinessRuleError<out E>(val error: E) : Result<Nothing, E>()
data object Loading : Result<Nothing, Nothing>()
fun isSuccessful() = this is Success
fun hasFailed() = this is Error || this is BusinessRuleError<*>
fun isLoading() = this is Loading
override fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$error]"
is BusinessRuleError<*> -> "BusinessRuleError[error=$error]"
Loading -> "Loading"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment