Created
June 18, 2021 17:51
-
-
Save juliancadi/ac7dff79ee986518e271c52c3f5f6eab to your computer and use it in GitHub Desktop.
Result monad for dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Result<Success, Error extends Exception> { | |
final Success? successIfAny; | |
final Error? errorIfAny; | |
const Result.success(this.successIfAny) : errorIfAny = null; | |
const Result.error(this.errorIfAny) : successIfAny = null; | |
Result<Success, Error> onSuccess(Function(Success) handler) { | |
if (successIfAny == null) return this; | |
handler(successIfAny as Success); | |
return this; | |
} | |
Result<Success, Error> onError(Function(Error) handler) { | |
if (errorIfAny == null) return this; | |
handler(errorIfAny as Error); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment