Skip to content

Error constructing struct record in member using 'with' #7536

@antrv

Description

@antrv

The following code is not compiled:

[<Struct>]
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:

[<Struct>]
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

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingArea-Compiler-Checking-Byrefbyref, inref, outref, byref-like types, NativePtr, function pointers, struct semanticsBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

Type

Projects

Status

New

Relationships

None yet

Development

No branches or pull requests

Issue actions