Error constructing struct record in member using 'with'
Open
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
Assessment
This issue has not been assessed yet.