In F#, use seems more readable to me, but using is more declarative in nature. Is there a general rule of thumb for which to favor in source?
I generally prefer use. The using function has been in the libraries in the times when use did not exist, so some older F# examples might use using even in cases when the use keyword would make more sense.
The using function might still make sense when you want to specify the scope more explicitly:
using (new StreamWriter(..)) (fun sw ->
sw.Write("hi"))
printfn "Something more here"
But even this can be written using use:
( use sw = new StreamWriter(..)
sw.Write("hi") )
printfn "Something more here"
Another nice thing about the use keyword is that you can use it inside sequence expressions (when generating collections e.g. from file content) and in asynchronous workflows (to manage connections allocated during network communication).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With