diff --git a/src/FSharpPlus/Extensions/Result.fs b/src/FSharpPlus/Extensions/Result.fs index a243a2a33..4f7208d9b 100644 --- a/src/FSharpPlus/Extensions/Result.fs +++ b/src/FSharpPlus/Extensions/Result.fs @@ -136,6 +136,24 @@ module Result = /// let defaultWith (defThunk: 'Error->'T) (result: Result<'T,'Error>) : 'T = match result with Ok v -> v | Error e -> defThunk e + /// Returns the first result if it is Ok, otherwise returns the second result. + /// The alternative result. + /// The source result. + /// The source result if it is Ok, otherwise the alternative result. + let orElse (alternative: Result<'T, 'Error>) (source: Result<'T, 'Error>) : Result<'T, 'Error> = + match source with + | Ok v -> Ok v + | Error _ -> alternative + + /// Returns the first result if it is Ok, otherwise invokes the alternative thunk to obtain an alternative result. + /// A thunk that provides an alternative result when invoked. + /// The source result. + /// The source result if it is Ok, otherwise the result of invoking the alternative thunk. + let orElseWith (alternativeThunk: 'Error -> Result<'T, 'Error>) (source: Result<'T, 'Error>) : Result<'T, 'Error> = + match source with + | Ok v -> Ok v + | Error e -> alternativeThunk e + /// Converts a Result<'T,'Error> to a Choice<'T,'Error>. let toChoice (source: Result<'T,'U>) = match source with Ok x-> Choice1Of2 x | Error x -> Choice2Of2 x