Result<S extends Object, F extends Object> class sealed

A Result represents the result of an operation. It is a Success or Failure.

Results are an alternative error-handling mechanism to exceptions. It is inspired by other languages such as Rust and Swift.

To represent a success:

Result<String, int> httpGet() {
  if (successful) return Success('HTTP body in plain text');
}

To represent a failure:

Result<String, int> httpGet() {
  if (notFound) return Failure(404);
}

You can also perform pattern matching on a Result.

void handle() {
  switch (httpGet()) {
    case Success(:final success):
      // handle success
    case Failure(:final failure):
      // handle failure
  }
}

See Maybe for representing a value and the possible absence thereof.

Implementers

Properties

failure → F?
The value if this is a Failure or null otherwise.
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
success → S?
The value if this is a Success or null otherwise.
no setter

Methods

bind<T extends Object>(Result<T, F> function(S success)) Result<T, F>
If this is a Success, maps S to Result, otherwise returns F untouched.
bindFailure<T extends Object>(Result<S, T> function(F failure)) Result<S, T>
If this is a Failure, maps F to Result, otherwise returns S untouched.
map<T extends Object>(T function(S success)) Result<T, F>
If this is a Success, maps S to T, otherwise returns F untouched.
mapFailure<T extends Object>(T function(F failure)) Result<S, T>
If this is a Failure, maps F to T, otherwise returns S untouched.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pipe<T extends Object>(Future<Result<T, F>> function(S success)) Future<Result<T, F>>
If this is a Success, asynchronously maps S to Result, otherwise returns F untouched.
pipeFailure<T extends Object>(Future<Result<S, T>> function(F failure)) Future<Result<S, T>>
If this is a Failure, asynchronously maps F to Result, otherwise returns S untouched.
toString() String
A string representation of this object.
inherited
when({Consume<S> success = _nothing, Consume<F> failure = _nothing}) → void
Calls success if this is a Success, or failure if this is a Failure.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

of<S extends Object, F extends Exception>(Supply<S> throwing) Result<S, F>
Wraps throwing, which may throw an exception, in a Result.