fsharp / fsharp/fslang-suggestions

Mutable contents in Anonymous Records

Open
#732 5 comments 21 reactions 0 assignees View on GitHub
approved-in-principle area: anonymous-records
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we allow for mutable contents in F# anonymous records. Today, it is not possible either as a declaration expression or when creating a new anonymous record:

image

(Note that the syntax in the screenshot is not necessarily what the syntax should be, it's just what felt the most natural to me)

Syntax could look like this:

```fsharp
// Declaration
let f (r: {| mutable Doot: string |}) = ()

// Instantiation
let _ = {| mutable Yoder: flat |}
```

This would be analogous to mutable contents in regular F# records.

The [spec](https://github.com/fsharp/fslang-design/blob/master/FSharp-4.6/FS-1030-anonymous-records.md) does not mention `mutable` labels, hence this suggestion.

You can have a type be a `ref` cell today, so you can still mutate:

image

However this does incur the additional allocation and readability costs of references cells.

## Pros and Cons

The advantages of making this adjustment to F# are:

* More parity with record types
* No more need to use `ref` cells if this is the pattern you're using

The disadvantages of making this adjustment to F# are:

* More avenues to mutation
* Is there a good instantiation syntax?

## Extra information

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

## Affidavit (please submit!)

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] 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.

Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the linked FSharp-4.6 anonymous-records specification and compare its treatment with mutable regular F# records. Evaluate syntax for mutable labels in declarations and instantiations, including the stated ref-cell alternative and pros and cons; done means the language-design position and any required specification changes are settled.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.