Skip to content

Add MergeSources to AsyncResultCE so we can use and! safely #305

@64J0

Description

@64J0

Is your feature request related to a problem? Please describe.

When using this script:

#r "nuget: FsToolkit.ErrorHandling, 4.18.0"

open FsToolkit.ErrorHandling

let ok1 () =
    printfn "Called ok1"
    Ok 1 |> async.Return

let err1 () : Async<Result<unit, string>> =
    printfn "Called err1"
    Error "1" |> async.Return

let ok2 () =
    printfn "Called ok2"
    Ok 2

let err2 () : Result<unit, string> =
    printfn "Called err2"
    Error "2"

let workflow1 () =
    asyncResult {
        let! _ = ok1 ()
        and! _ = err1 ()
        and! _ = ok2 ()
        and! _ = err2 ()

        printfn "workflow1 finished"
    }

let workflow2 () =
    asyncResult {
        let! _ = ok1 ()
        let! _ = err1 ()
        let! _ = ok2 ()
        let! _ = err2 ()

        printfn "workflow2 finished"
    }

async {
    printfn "Using let! and and! ...\n"
    let! res1 = workflow1 ()
    printfn "%A" res1

    printfn "\n\nUsing only let! ...\n"
    let! res2 = workflow2 ()
    printfn "%A" res2

    return ()
}
|> Async.RunSynchronously

I noticed that all the values are evaluated inside the asyncResult CE with no short-circuit being applied (what I mean by short-circuit is the mechanism that returns immediately after a Result.Error appears in the case of the asyncResult CE).

After executing this script I get this log at the console:

Using let! and and! ...

Called ok1
Called err1
Called ok2
Called err2
Error "1"


Using only let! ...

Called ok1
Called err1
Error "1"

Describe the solution you'd like

It would be great to have the short-circuit when using the and! for AsyncResult CE.

Describe alternatives you've considered

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions