flatten method

Result<T, E> flatten()

Flattens a nested Result into a single Result.

Converts Result<Result<T, E>, E> into Result<T, E>. Use this when you have a result that contains another result, such as one created with map or explicit nested construction. andThen already flat-maps the result returned by its callback. This unwraps one level of nesting.

Example:

final nested = Ok(Ok(30));
final flat = nested.flatten(); // Ok(30)

final nested2 = Ok(Err('error'));
final flat2 = nested2.flatten(); // Err('error')

Implementation

Result<T, E> flatten() => switch (this) {
  Ok(:final value) => value,
  Err(:final error, :final stackTrace) => .err(error, stackTrace),
};