mapOrElse<R> method

R mapOrElse<R>(
  1. R fallback(
    1. E error
    ),
  2. R mapper(
    1. T value
    )
)

Maps a successful value, or computes a fallback from the error.

Only the branch that applies is called.

final label = const Err<int, String>('offline')
    .mapOrElse((error) => 'failed: $error', (n) => '$n items');

Implementation

R mapOrElse<R>(R Function(E error) fallback, R Function(T value) mapper) {
  return switch (this) {
    Ok<T, E>(:final value) => mapper(value),
    Err<T, E>(:final error) => fallback(error),
  };
}