expect method

T expect(
  1. String message
)

Returns the present value, or throws message when absent.

Throws a StateError carrying message for None, so the failure says why the value was expected rather than only that it was missing.

final user = const Some<String>('John').expect('the session has a user');

Implementation

T expect(String message) {
  return switch (this) {
    Some<T>(:final value) => value,
    None<T>() => throw StateError(message),
  };
}