orElse<F extends Object> method

Result<T, F> orElse<F extends Object>(
  1. Result<T, F> otherBlock(
    1. E error,
    2. StackTrace st
    )
)

Returns this success value, or computes a recovery result from the error.

If this is Ok, returns it unchanged. If this is Err, applies otherBlock to the error and stack trace, returning the computed result. Use this for dynamic recovery that depends on what the error was. For static fallbacks, use or instead.

This allows context-aware error handling—examine the error and decide whether to recover or propagate a different result.

Example:

final result = operation.orElse(
  (error, st) => error is NotFoundException
    ? Ok(defaultValue)
    : Err(error),
);

Implementation

Result<T, F> orElse<F extends Object>(
  Result<T, F> Function(E error, StackTrace st) otherBlock,
) => switch (this) {
  Err(:final error, :final stackTrace) => otherBlock(error, stackTrace),
  Ok(:final value) => .ok(value),
};