andThen<R> method
Chains another result-producing operation when this result is successful.
Use this when the next step can also fail.
Result<int, String> parseCount(String text) {
final value = int.tryParse(text);
return value == null ? const Err('invalid') : Ok(value);
}
Result<int, String> requirePositive(int value) {
return value > 0 ? Ok(value) : const Err('must be positive');
}
final parsed = parseCount('2').andThen(requirePositive);
final failed = parseCount('-1').andThen(requirePositive);
Implementation
Result<R, E> andThen<R>(Result<R, E> Function(T value) next) {
return switch (this) {
Ok<T, E>(:final value) => next(value),
Err<T, E>(:final error) => Err<R, E>(error),
};
}