From 4c12451d734c47ee2c561d87d02d0d301fc42422 Mon Sep 17 00:00:00 2001 From: Joseph Price Date: Sat, 11 Jan 2025 14:18:42 -0500 Subject: [PATCH] add requireSomeWith doc --- gitbook/result/requireFunctions.md | 8 ++++---- src/FsToolkit.ErrorHandling/Result.fs | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/gitbook/result/requireFunctions.md b/gitbook/result/requireFunctions.md index 31b152dd..3c642edc 100644 --- a/gitbook/result/requireFunctions.md +++ b/gitbook/result/requireFunctions.md @@ -64,9 +64,9 @@ let result : Result = // Error "Value must be false" ``` -## requireSome +## requireSomeWith -Converts an Option to a Result, using the given error if None. +Converts an Option to a Result, using the given function to supply an error if None. ### Function Signature @@ -81,7 +81,7 @@ Converts an Option to a Result, using the given error if None. ```fsharp let result : Result = Some 1 - |> Result.requireSome "Value must be Some" + |> Result.requireSomeWith (fun () -> "Value must be Some") // Ok () ``` @@ -91,7 +91,7 @@ let result : Result = ```fsharp let result : Result = None - |> Result.requireSome "Value must be Some" + |> Result.requireSomeWith (fun () -> "Value must be Some") // Error "Value must be Some" ``` diff --git a/src/FsToolkit.ErrorHandling/Result.fs b/src/FsToolkit.ErrorHandling/Result.fs index 3ccf7b37..d772746c 100644 --- a/src/FsToolkit.ErrorHandling/Result.fs +++ b/src/FsToolkit.ErrorHandling/Result.fs @@ -307,6 +307,14 @@ module Result = | Some x -> Ok x | None -> Error error + /// + /// Returns the contained value if Some, otherwise evaluates and returns the value. + /// + /// Documentation is found here: https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#requireSomeWith + /// + /// The function to evaluate if the result is Error. + /// The input result. + /// The contained value if Some, otherwise the result of evaluating . let inline requireSomeWith (ifErrorThunk: unit -> 'error) (option: 'ok option)