transposed property

  1. @useResult
Result<Option<T>, E> transposed

An Option containing a Result transposed into a Result containing an Option.

A None will be mapped to an Ok with a None value. A Some with an Ok value will be mapped to an Ok with a Some value, and a Some with an Err value will be mapped to an Err.

Examples

// prints "Ok(Some(2))"
print(const Some(Ok<int, String>(2)).transposed);

// prints "Err(error)"
print(const Some(Err<int, String>('error')).transposed);

Implementation

@useResult
Result<Option<T>, E> get transposed {
  return switch (this) {
    // ignore: unused_result
    Some(:final value) => value.map((value) => Some(value)),
    None() => Ok(None<T>()),
  };
}