dotnet / dotnet/fsharp

Error constructing struct record in member using 'with'

Open
#7,536 6 comments 3 reactions 1 assignee Claimed by @edgarfgp View on GitHub
Area-Compiler-Checking Area-Compiler-Checking-Byref Bug Impact-Medium
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

The following code is not compiled:

```
[]
type Person =
{ Name: string; Age: int }
member x.WithAge age =
{ x with Age = age }
```

with the error FS3232: Struct members cannot return the address of fields of the struct by reference, pointing to the `x` inside `WithAge` member.

The problem is that x has type inref, but nothing prevents from constructing another record instance from it.

The known workaround is to declare another variable from `x`:
```
[]
type Person =
{ Name: string; Age: int }
member x.WithAge age =
let copy = x
{ copy with Age = age }
```

Environment:
* .NET Core 2.2
* VS 2019 16.2.5

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.