Does copying and updating for immutable record types in a shared folder or copying F #?

Is the copy and update procedure for immutable entries in the F # folder or copy memory? Value in the following code

type MyRecord = { X: int; Y: int; Z: int } let myRecord1 = { X = 1; Y = 2; Z = 3; } let myRecord2 = { myRecord1 with Y = 100; Z = 2 } 

do myRecord1 and myRecord2 share memory for variable X ? More generally, is there a good reference that indicates which immutable / persistent data structures in F # are actively exchanging memory?

+7
functional-programming f #
source share
1 answer

In this case, the memory for variable X will be copied. The last line of your code is just another way to write this:

 let myRecord2 = { X = myRecord1.X; Y = 100; Z = 2 } 

Now, if X has a reference type, the memory for the reference to it would be copied, but the memory for its contents would be shared.

For example, consider the following:

 type MyX = { W: int; U: int } type MyRecord = { X: MyX; Y: int; Z: int } let myRecord1 = { X = { W = 5; U = 6 }; Y = 2; Z = 3; } let myRecord2 = { myRecord1 with Y = 100; Z = 2 } 

In the above code, the memory for X will be copied, but the memory for W and U will be shared.

+9
source share

All Articles