transposed property
A Result
containing an Option transposed into an Option containing a Result
.
An Ok with a None value will be mapped to a None, and an Ok with a Some value will be mapped to a Some with an Ok value. An Err will be mapped to a Some with an Err value.
Examples
// prints "Some(Ok(2))"
print(const Ok<Option<int>, String>(Some(2)).transposed);
// prints "Some(Err('error'))"
print(const Err<Option<int>, String>('error').transposed);
Implementation
@useResult
Option<Result<T, E>> get transposed {
return switch (this) {
// ignore: unused_result
Ok(:final value) => value.map<Result<T, E>>((value) => Ok<T, E>(value)),
Err(:final error) => Some(Err(error)),
};
}