Skip to content

Mutable use bindings permitted by the F# compiler #535

Closed

Description

@eiriktsarpalis

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?

Activity

  1. dsyme commented on Jul 17, 2015

    @dsyme
    Contributor

    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.

  2. dsyme commented on Jul 17, 2015

    @dsyme
    Contributor

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

  3. dsyme commented on Aug 2, 2016

    @dsyme
    Contributor

    @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>

  4. reopened this on Aug 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions