dotnet / dotnet/fsharp

Copy and update inside a folder function with an anonymous record prints a misleading error message

Open
#12,489 3 comments 1 reaction 0 assignees View on GitHub
Area-Diagnostics Area-LangService-Diagnostics Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

When using an anonymous record as the initial state of a fold, I noticed that I can't use copy and update expressions in the folder function. See this screenshot:

![image](https://user-images.githubusercontent.com/56472600/144812249-1f4f6ca2-5070-401f-80d2-b50d1eb1f965.png)

After a bit of troubleshooting on the FSSF Slack server, we noticed that the anonymous record has to be constructed "fully" once, and then the copy and update syntax is available.

![image](https://user-images.githubusercontent.com/56472600/144812384-6be31b2d-cb0a-4282-b317-47a0887c3ca6.png)

Code snippet:

```f#
type Command =
| Add of int
| Sub of int

let anonymousRecordFoldMatchBug x =
let commandList = [Add 3; Sub 1; Add 10]
commandList
|> List.fold (fun state value ->
match value with
| Add x -> {| Result = state.Result + x |}
| Sub x -> {| state with Result = state.Result + x |})
{| Result=0 |}
```

**Expected behavior**

Being able to use the copy and update syntax from the start, or at least a more explicative error message that points out the steps needed to fix the problem.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.