Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't a computation expression span the finally block

Tags:

For example:

let test () = 
    async {
        try 
           do! someting ()
        finally
           do! sometingElse ()
    }

You can't do the do! in the finally you get the message from the compiler "you can only use do! in a computation expression" but it's still in there right.

I know how I can solve this but I would like to understand why the compiler restricts this scenario.

Ok after some fiddeling I think it's desugared like this: (I'm very happy we can write cexprs)

from:

async {
    try 
       do! someting ()
       do! sometingElse ()
    finally
       printfn "finally"
}

to:

async.TryFinally(
    async.Bind(
        someting(), (fun () -> 
            async.Bind(sometingElse (), (fun () -> 
                async.Zero())))), (fun () -> printfn "finally")) |> ignore

I get that the second part of the TryFinally doesn't support a cexpr.