F # async - difference between two structures

Is there a difference between writing something like this:

MailboxProcessor.Start(fun inbox -> async {
    let rec loop bugs =
        let! msg = inbox.Receive()
        let res = //something
        loop res
    loop []})

And I write it like this:

MailboxProcessor.Start(fun inbox ->
    let rec loop bugs = async {
        let! msg = inbox.Receive()
        let res = //something
        do! loop res }
    loop [])

Thank!

+5
source share
1 answer

The first example is not valid with the F # code, because it let!can only be used directly in the calculation expression. In your example, you use it in a regular function - its body is not an expression of calculation, therefore let!it is not allowed in this position.

To make it valid, you need to wrap the body of the function loopinside async:

MailboxProcessor.Start(fun inbox -> async {
    let rec loop bugs = async {
        let! msg = inbox.Receive()
        let res = //something
        return! loop res }
    return! loop []})

async { .. } - return! loop , ( ).

, return! do! - , return! , , . do!, async - , do! .

+7

All Articles