Skip to content

Instantly share code, notes, and snippets.

@juliancadi
Created June 18, 2021 17:51
Show Gist options
  • Save juliancadi/ac7dff79ee986518e271c52c3f5f6eab to your computer and use it in GitHub Desktop.
Save juliancadi/ac7dff79ee986518e271c52c3f5f6eab to your computer and use it in GitHub Desktop.
Result monad for dart
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