From d10cc0db3015ede699f79079c98d030d676f52a8 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Sun, 11 Jan 2026 10:06:21 +0100 Subject: [PATCH] Add orElse /orElseWith to Result --- src/FSharpPlus/Extensions/Result.fs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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