andThen<U extends Object?> method
Transforms the success value into a new result, or propagates the error.
If this is Ok, applies otherBlock to the value and returns its result.
If this is Err, propagates the error unchanged (short-circuit behavior).
Stack traces are preserved through error propagation.
This is the monadic bind operation (flatMap), useful for chaining operations that might fail while maintaining error information throughout the chain. Use map for simple value transformations that can't fail.
Example:
final result = getUserId()
.andThen((id) => getUser(id))
.andThen((user) => loadPreferences(user.id));
// If any step fails, subsequent steps are skipped
Implementation
Result<U, E> andThen<U extends Object?>(
Result<U, E> Function(T value) otherBlock,
) => switch (this) {
Ok(:final value) => otherBlock(value),
Err(:final error, :final stackTrace) => .err(error, stackTrace),
};