orElse Functions

Result.orElse

Namespace: FsToolkit.ErrorHandling

Returns the result if the result is Ok, otherwise returns the given result

Function Signature

Result<'ok, 'errorOutput> -> Result<'ok, 'error> 
    -> Result<'ok, 'errorOutput>

Examples

Example 1

let result : Result<int, string> =
    Ok 1
    |> Result.orElse (Ok 2)
    
// Ok 1

Example 2

let result : Result<int, string> =
    Ok 1
    |> Result.orElse (Error "Error")
    
// Ok 1

Example 3

let result : Result<int, string> =
    Error "Error"
    |> Result.orElse (Ok 2)
    
// Ok 2

Example 4

let result : Result<int, string> =
    Error "Error"
    |> Result.orElse (Error "Error 2")

// Error "Error 2"

Result.orElseWith

Namespace: FsToolkit.ErrorHandling

Returns the result if the result is Ok, otherwise evaluates the given function and returns the result.

Function Signature

('error -> Result<'ok, 'errorOutput>) -> Result<'ok, 'error> 
    -> Result<'ok, 'errorOutput>

Examples

Example 1

let result : Result<int, int> =
    Ok 1
    |> Result.orElseWith (fun e -> e + 1 |> Ok)

// Ok 1

Example 2

let result : Result<int, int> =
    Ok 1
    |> Result.orElseWith (fun e -> e + 1 |> Error)

// Ok 1

Example 3

let result : Result<int, int> =
    Error 1
    |> Result.orElseWith (fun e -> e + 1 |> Ok)

// Ok 2

Example 4

let result : Result<int, int> =
    Error 1
    |> Result.orElseWith (fun e -> e + 1 |> Error)

// Error 2

Last updated