fn: fix Result[T].FlatMap

For a Result[T], FlatMap should apply f when the result is Ok, and
propagate the error unchanged when it's Err. The original code returns r
on Ok and tries to use r.left when Err, which is wrong. This commit
fixes that.

Secondly, the group of FlatMap/AndThen and OrElse functions and methods
are now properly tested with new unit tests.

fixes #10401
This commit is contained in:
Gijs van Dam committed 2026-01-06 10:51:49 +01:00
1 parent 801de79cf1
commit e7b9515edd
2 files changed
+129 -2

No files matched your search

+2 -2
View File
@@ -149,10 +149,10 @@ func FlattenResult[A any](r Result[Result[A]]) Result[A] {
// success value if it exists.
func (r Result[T]) FlatMap(f func(T) Result[T]) Result[T] {
if r.IsOk() {
return r
return f(r.left)
}
return f(r.left)
return r
}
// AndThen is an alias for FlatMap. This along with OrElse can be used to