Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use vs Using: preferred style? [closed]

Tags:

.net

f#

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?

like image 596
jcc333 Avatar asked Sep 09 '26 09:09

jcc333


1 Answers

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).

like image 151
Tomas Petricek Avatar answered Sep 11 '26 12:09

Tomas Petricek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!