-
Notifications
You must be signed in to change notification settings - Fork 815
Error constructing struct record in member using 'with' #7536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This error is to be expected - no longer making [<Struct>]
type Person =
{ Name: string; Age: int }
module Person =
let withAge age p = { p with Age = age } |
@cartermp, you wrote:
However, I have come to understand that the
I don't understand why your module code creates a copy, but the OP's code does not. In his code, I don't see any reference, or is the 'this' pointer an implicit reference here and needs it (implicitly) to be dereferenced first by copying it locally? For non struct records, that syntax would just copy and update the record and return a new copy. If there are differences in behavior, perhaps we can update the docs? And maybe also the error? |
The 'this' pointer is an |
@cartermp, thanks for the explanation. I reread the byref docs, and it's included that a readonly struct is treated as However, since the
The error mentioned by the OP doesn't appear to be very specific to the situation, esp since the code doesn't show any attempt to return an address. |
This error surprises me, I can't immediately imagine why it's being triggered. We should have de-sugared to a TAST like this: member x.WithAge age =
TOp.Recd ( args = [ x.Name; age ] ) and I would have thought this would allow the But clearly there's something awry. |
@dsyme, I suggest we reopen this? Your comment makes me believe this is a bug after all. BTW, I just recollect that I have code like the one of the OP that just compiles as expected, perhaps the structness of this one causes the unexpected behavior? Something with boxing of the |
The following code is not compiled:
with the error FS3232: Struct members cannot return the address of fields of the struct by reference, pointing to the
x
insideWithAge
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
:Environment:
The text was updated successfully, but these errors were encountered: