19

According to the last sentence on this MSDN page use is to be preferred over using. I've heard it elsewhere (this answer, for example). Why is this? I realize use was added later. But what's the difference? On the surface, using seems more useful because you can control when Dispose() is called, and you can explicitly ignore the bound value (e.g., (fun _ -> ...)) if needed.

Community
  • 1
  • 1
Daniel
  • 47,404
  • 11
  • 101
  • 179

4 Answers4

21

You can control when dispose is called with use as well, just by using usual scoping contructs (like parens or begin-end), e.g.

let F() =
    let x = 4
    (
        use file = System.IO.File.Open("foo.xml", System.IO.FileMode.Append)
        let z = 4
        printfn "file still open here"
    )
    printfn "file was already closed/disposed"

But I think this is rarely useful. I think it is also rare to not want to name/utilize the IDisposable object. use is more syntactically convenient, and 95% of the time does what you need, so I think that's why it's preferred.

Brian
  • 117,631
  • 17
  • 236
  • 300
  • 2
    The syntactic oddity of this seems like yet another reason to favor `using`. Do you know if they produce different IL? – Daniel Feb 18 '11 at 21:25
  • I expect they produce similar IL, though I don't know or care. As for the syntactical oddity, again, no one write code like this, because no one cares whether `Dispose` is called two lines before the end of the function or just at the end of the function. Scenarios where it matters are extremely rare. Just use `use`. – Brian Feb 18 '11 at 22:20
  • One scenario where it does require you to write something _after_ the `use` scope ends is if you need to properly test for a released object (a locked file being writable again), or for instance with semaphores. Still, just wrapping the `use` in one function and call your after-release code after calling that function would suffice, and scoping like in this example is not needed. – Abel Nov 04 '14 at 18:35
  • It's interesting to note that everybody seems to be missing the `use ... in ...` syntax to control scoping. I didn't even know that you can do that with parens like you show; I thought the only way is to use `in`. For example: https://gist.github.com/jwosty/cfbf9cc67e9d051f2194895d37298cf9 – Jwosty Jun 26 '19 at 15:17
16

I think that the reason for preferring use is just that the syntax is simpler. Many other language constructs could be expressed as functions (e.g. try .. with, for, while, ...). If the language designers added a simpler syntax, why not use it...

As I wrote in the earlier answer you referenced, you can precisely control the scope even when using use. (And this way, you can use it even in constructors of object expressions class declarations.) But most of the time, the automatic behavior is just fine (which makes the construct simpler than using in C#).

Whether you'll use use or using in situations where you need to control the scope explicitly is a matter of personal taste. If you don't like the explicit scoping of use (which looks a bit weird, I admit, but works fine for me), you can use using.

EDIT: In a class declaration, you cannot for example write:

type Foo() =
  use a = new Whatever()
  // ...

because the scope of a would be (possibly) the whole lifetime of the instance. (Although I think this could be useful and it could add automatic implementation of IDisposable to your type). If you use using, you don't get this sort of trouble.

Community
  • 1
  • 1
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
3

Personally, I prefer use to using for the same reason that I prefer

let a = some_expr
some_stuff_with_a

to

(fun a -> some_stuff_with_a) some_expr

With the binding form, you can typically avoid a set of parentheses, and the association between the identifier and the value that it's being bound to are closer in space and easier to see.

kvb
  • 54,864
  • 2
  • 91
  • 133
0

An example against use is better then using:

using is better than use as using can be written in one line while use cannot.

Example,
xx is a function returning a value by a function fct from a resource which is opened by yy using given parameter p.

let xx p = using (yy(p)) (fun resource-> fct resource)     // <-- this is OK

let xx p  = (use resource = yy(p); fct resource)           // <-- this is Not OK
pinckerman
  • 4,115
  • 6
  • 33
  • 42