In F# async
workflows, we can define a resource that should be cleaned up with the use
keyword.
But how does use
interact with return
?
For example, given this code:
let createResource = async {
use r = Resource ()
do! operationThatMightThrow r
return r
}
async {
use! r = createResource
printfn "%O" r
}
|> Async.RunSynchronously
Where will the calls to Resource.Dispose
happen?
How can I design this so that the r
is always cleaned up (even if operationThatMightThrow
throws)?