Skip to content

Commit

Permalink
add requireSomeWith doc
Browse files Browse the repository at this point in the history
  • Loading branch information
joprice committed Jan 11, 2025
1 parent f5a1c85 commit 4c12451
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions gitbook/result/requireFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ let result : Result<unit, string> =
// 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

Expand All @@ -81,7 +81,7 @@ Converts an Option to a Result, using the given error if None.
```fsharp
let result : Result<unit, string> =
Some 1
|> Result.requireSome "Value must be Some"
|> Result.requireSomeWith (fun () -> "Value must be Some")
// Ok ()
```
Expand All @@ -91,7 +91,7 @@ let result : Result<unit, string> =
```fsharp
let result : Result<unit, string> =
None
|> Result.requireSome "Value must be Some"
|> Result.requireSomeWith (fun () -> "Value must be Some")
// Error "Value must be Some"
```
Expand Down
8 changes: 8 additions & 0 deletions src/FsToolkit.ErrorHandling/Result.fs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ module Result =
| Some x -> Ok x
| None -> Error error

/// <summary>
/// Returns the contained value if <c>Some</c>, otherwise evaluates <param name="ifErrorThunk"/> and returns the value.
///
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/others#requireSomeWith</href>
/// </summary>
/// <param name="ifErrorThunk">The function to evaluate if the result is <c>Error</c>.</param>
/// <param name="option">The input result.</param>
/// <returns>The contained value if <c>Some</c>, otherwise the result of evaluating <paramref name="ifErrorThunk"/>.</returns>
let inline requireSomeWith
(ifErrorThunk: unit -> 'error)
(option: 'ok option)
Expand Down

0 comments on commit 4c12451

Please sign in to comment.