expect method

T expect(
  1. String message
)

Returns the successful value, or throws message for an Err.

Throws a StateError carrying message followed by the error, so the failure says both why success was expected and what went wrong.

final port = const Ok<int, String>(8080).expect('the config has a port');

Implementation

T expect(String message) {
  return switch (this) {
    Ok<T, E>(:final value) => value,
    Err<T, E>(:final error) => throw StateError('$message: $error'),
  };
}