Skip to content

mutable on any binding #1457

@Happypig375

Description

@Happypig375

I propose we allow any pattern binding to be mutable.

let f (mutable d : System.Collections.Generic.Dictionary<int, int>) = // mutable parameter bindings
    for KeyValue (mutable k, mutable v) in d do
        // use k and v in an imperative algorithm...
        v <- v + 1

The existing let mutable x = syntax is equivalent to let (mutable x) =. Self identifiers should not be allowed mutable to be consistent with C#.

The existing way of approaching this problem in F# is to do an explicit copy.

let f (d : System.Collections.Generic.Dictionary<int, int>) =
    let mutable d = d // here
    for KeyValue (k, v) in d do
        let mutable k = k // here
        let mutable v = v // here
        // use k and v in an imperative algorithm...
        v <- v + 1

This is undesirable for large structs which could have avoided an unnecessary copy.

Pros and Cons

The advantages of making this adjustment to F# are

  1. Less boilerplate
  2. More optimization opportunities

The disadvantage of making this adjustment to F# is the encouragement of using mutables in a functional programming language. However, F# is multi-paradigm, and we use imperative programming where suitable, right?

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick these items by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
  • This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

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