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 writing it like this:
MailboxProcessor.Start(fun inbox ->
let rec loop bugs = async {
let! msg = inbox.Receive()
let res = //something
do! loop res }
loop [])
Thanks!
The first example is not valid F# code, because
let!can only be used immediately inside computation expression. In your example, you’re using it in an ordinary function – its body is not a computation expression, solet!is not allowed in that position.To make it valid, you’d need to wrap the body of the
loopfunction insideasync:You can keep the outer
async { .. }block in the snippet as well – then you just need to usereturn!to call yourloopfunction instead of just returning it (but other than that there is no significant difference now).Note that I used
return!instead ofdo!– this actually makes a difference, becausereturn!represents a tail-call, which means that the rest of the current body can be discarded. If you usedo!then the async allocates something like a stack frame in the heap, so usingdo!in a recursive looping function leaks memory.