Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutable use bindings permitted by the F# compiler #535

Closed
eiriktsarpalis opened this issue Jul 17, 2015 · 4 comments
Closed

Mutable use bindings permitted by the F# compiler #535

eiriktsarpalis opened this issue Jul 17, 2015 · 4 comments

Comments

@eiriktsarpalis
Copy link
Member

I just noticed that the F# compiler permits mutable use bindings:

let disp msg = { new System.IDisposable with member __.Dispose () = printfn "%s" msg }

let test () =
    use mutable d = disp "foo"
    d <- disp "bar"

Running test will cause "bar" to be printed. This feels wrong and the sentiment was corroborated by the fact that C# explicitly prevents it:

using (MemoryStream m = new MemoryStream())
{
      m = null;
}

which produces the compiler error:

Error   1   Cannot assign to 'm' because it is a 'using variable'

I checked the F# spec, but couldn't find any explicit mention of mutable use bindings.

Is this a compiler bug then?

@dsyme
Copy link
Contributor

dsyme commented Jul 17, 2015

It's by spec but I'd be happy to emit a warning in this case. Could you add an entry to http://fslang.uservoice.com? I'll mark it as approved for a future edition of the language.

@dsyme
Copy link
Contributor

dsyme commented Jul 17, 2015

Closing this since it's now recoded on uservoice (and approved)

@dsyme dsyme closed this as completed Jul 17, 2015
@dsyme
Copy link
Contributor

dsyme commented Aug 2, 2016

@eiriktsarpalis An update on this: I believe mutable use bindings may be needed for F# in the case that the "disposable" type is a struct. For example, a struct enumerator of a System.Collections.Generic.List<T>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants