Skip to content

Commit e9f7340

Browse files
sep2TheAngryByrd
authored andcommitted
Add defaultError and zipError
1 parent 0dbcb8f commit e9f7340

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/FsToolkit.ErrorHandling/Result.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ module Result =
117117
| Ok x -> x
118118
| Error _ -> ifError
119119

120+
// Returns the contained value if Error, otherwise returns ifOk.
121+
let defaultError ifOk result =
122+
match result with
123+
| Error error -> error
124+
| Ok _ -> ifOk
125+
120126
/// Returns the contained value if Ok, otherwise evaluates ifErrorThunk and
121127
/// returns the result.
122128
let defaultWith ifErrorThunk result =
@@ -178,4 +184,11 @@ module Result =
178184
match x1,x2 with
179185
| Ok x1res, Ok x2res -> Ok (x1res, x2res)
180186
| Error e, _ -> Error e
181-
| _, Error e -> Error e
187+
| _, Error e -> Error e
188+
189+
/// Takes two results and returns a tuple of the error pair
190+
let zipError x1 x2 =
191+
match x1, x2 with
192+
| Error x1res, Error x2res -> Error(x1res, x2res)
193+
| Ok e, _ -> Ok e
194+
| _, Ok e -> Ok e

tests/FsToolkit.ErrorHandling.Tests/Result.fs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,18 @@ let defaultValueTests =
377377
]
378378

379379

380+
let defaultErrorTests =
381+
testList "defaultError Tests" [
382+
testCase "defaultError returns the error value" <| fun _ ->
383+
let v = Result.defaultError 43 (Error 42)
384+
Expect.equal v 42 ""
385+
386+
testCase "defaultError returns the given value for Ok" <| fun _ ->
387+
let v = Result.defaultError 43 (Ok 42)
388+
Expect.equal v 43 ""
389+
]
390+
391+
380392
let defaultWithTests =
381393
testList "defaultWith Tests" [
382394
testCase "defaultWith returns the ok value" <| fun _ ->
@@ -565,6 +577,22 @@ let zipTests =
565577
Expect.equal actual (Error "Bad1") "Should be Error"
566578
]
567579

580+
let zipErrorTests =
581+
testList "zipError tests" [
582+
testCase "Ok, Ok" <| fun () ->
583+
let actual = Result.zipError (Ok 1) (Ok 2)
584+
Expect.equal actual (Ok (1)) "Should be ok"
585+
testCase "Ok, Error" <| fun () ->
586+
let actual = Result.zipError (Ok 1) (Error "Bad")
587+
Expect.equal actual (Ok 1) "Should be ok"
588+
testCase "Error, Ok" <| fun () ->
589+
let actual = Result.zipError (Error "Bad") (Ok 1)
590+
Expect.equal actual (Ok 1) "Should be ok"
591+
testCase "Error, Error" <| fun () ->
592+
let actual = Result.zipError (Error "Bad1") (Error "Bad2")
593+
Expect.equal actual (Error ("Bad1", "Bad2")) "Should be Error"
594+
]
595+
568596
let allTests = testList "Result Tests" [
569597
resultIsOk
570598
resultIsError
@@ -592,6 +620,7 @@ let allTests = testList "Result Tests" [
592620
setErrorTests
593621
withErrorTests
594622
defaultValueTests
623+
defaultErrorTests
595624
defaultWithTests
596625
ignoreErrorTests
597626
teeTests
@@ -601,4 +630,5 @@ let allTests = testList "Result Tests" [
601630
sequenceAsyncTests
602631
valueOrTests
603632
zipTests
633+
zipErrorTests
604634
]

0 commit comments

Comments
 (0)