F# copy by value and reference
last modified December 2, 2026
In this article we look at what happens in F# when a value is bound to a second name or passed to a function: whether the data is copied, or whether both names refer to the same object.
.NET has two kinds of types. A value type, such as int,
float, bool or a struct, holds its data directly, so
binding it to another name copies the data. A reference type, such
as string, a record, or a reference cell, lives on the heap;
binding it to another name copies only the reference, and both names point to
the same object. With immutable data the difference cannot be observed. It
starts to matter as soon as an object has mutable parts.
Copying immutable values
Most F# values are immutable. When the data cannot change, it does not matter whether a binding copies the data or shares it.
let a = 10 let b = a let word = "falcon" let other = word let c = b + 5 let upper = other.ToUpper() printfn "a = %d, b = %d, c = %d" a b c printfn "word = %s, other = %s, upper = %s" word other upper
We bind an integer and a string to second names, then compute new values from them.
let a = 10 let b = a
The integer is a value type, so b gets its own copy of
10.
let word = "falcon" let other = word
A string is a reference type, so other refers to the same string
object as word. .NET strings are immutable: the
ToUpper method returns a new string and leaves the original
alone. Neither binding can change the data the other one sees.
λ dotnet fsi immutable_copy.fsx a = 10, b = 10, c = 15 word = falcon, other = falcon, upper = FALCON
Copying a struct
To see the difference, we need data that can change. A record with the
[<Struct>] attribute is a value type; its fields are stored
in the binding itself.
[<Struct>]
type Point = { mutable X: int; mutable Y: int }
let mutable p1 = { X = 1; Y = 2 }
let mutable p2 = p1
p2.X <- 10
p2.Y <- 20
printfn "p1: (%d, %d)" p1.X p1.Y
printfn "p2: (%d, %d)" p2.X p2.Y
We copy a struct point and change the fields of the copy.
let mutable p1 = { X = 1; Y = 2 }
let mutable p2 = p1
The second binding receives a copy of both fields. The bindings are declared
mutable because a struct field can only be changed through a
mutable binding; the fields are part of the binding's value.
p2.X <- 10 p2.Y <- 20
The assignments change only p2; p1 keeps its
original fields.
λ dotnet fsi struct_copy.fsx p1: (1, 2) p2: (10, 20)
Copying a record reference
Without the attribute, a record is a reference type. The same program now behaves differently.
type Point = { mutable X: int; mutable Y: int }
let p1 = { X = 1; Y = 2 }
let p2 = p1
p2.X <- 10
p2.Y <- 20
printfn "p1: (%d, %d)" p1.X p1.Y
printfn "p2: (%d, %d)" p2.X p2.Y
The Point type has the same fields, but it is an ordinary record.
let p1 = { X = 1; Y = 2 }
let p2 = p1
The record expression creates one object on the heap. The second binding
copies the reference to it, not the object, so p1 and
p2 are two names for the same record. The bindings themselves do
not need to be mutable, because we change the object, not the bindings.
p2.X <- 10 p2.Y <- 20
The change made through p2 is visible through p1.
λ dotnet fsi record_copy.fsx p1: (10, 20) p2: (10, 20)
Structural and reference equality
The = operator compares records by their fields. To find out
whether two names refer to the same object, we use
obj.ReferenceEquals.
type Point = { mutable X: int; mutable Y: int }
let p1 = { X = 1; Y = 2 }
let p2 = p1
let p3 = { X = 1; Y = 2 }
printfn "p1 = p2: %b" (p1 = p2)
printfn "p1 = p3: %b" (p1 = p3)
printfn "p1 and p2 same object: %b" (obj.ReferenceEquals(p1, p2))
printfn "p1 and p3 same object: %b" (obj.ReferenceEquals(p1, p3))
p3.X <- 5
printfn "after change, p1 = p3: %b" (p1 = p3)
printfn "p1: (%d, %d), p3: (%d, %d)" p1.X p1.Y p3.X p3.Y
The program compares a shared reference with a separate record that has the same field values.
printfn "p1 = p2: %b" (p1 = p2) printfn "p1 = p3: %b" (p1 = p3)
Both comparisons are true: records have
structural equality, so two records with equal fields are equal,
even when they are separate objects.
printfn "p1 and p2 same object: %b" (obj.ReferenceEquals(p1, p2)) printfn "p1 and p3 same object: %b" (obj.ReferenceEquals(p1, p3))
obj.ReferenceEquals tells the two cases apart: p2 is
the same object as p1, while p3 is a different one.
p3.X <- 5
Because p3 is a separate object, changing it leaves
p1 untouched, and the two records are no longer equal.
λ dotnet fsi equality.fsx p1 = p2: true p1 = p3: true p1 and p2 same object: true p1 and p3 same object: false after change, p1 = p3: false p1: (1, 2), p3: (5, 2)
Passing values to functions
F# passes arguments by value: a function receives a copy of what the argument holds. For a struct this is a copy of the data; for a record it is a copy of the reference, which still points to the caller's object.
type Point = { mutable X: int; mutable Y: int }
[<Struct>]
type SPoint = { mutable SX: int; mutable SY: int }
let moveRight (p: Point) =
p.X <- p.X + 100
let moveRightS (p: SPoint) =
let mutable copy = p
copy.SX <- copy.SX + 100
printfn "inside moveRightS: (%d, %d)" copy.SX copy.SY
let p = { X = 1; Y = 2 }
moveRight p
printfn "record after moveRight: (%d, %d)" p.X p.Y
let s = { SX = 1; SY = 2 }
moveRightS s
printfn "struct after moveRightS: (%d, %d)" s.SX s.SY
We pass a record point and a struct point to functions that move the point to the right.
let moveRight (p: Point) =
p.X <- p.X + 100
The parameter refers to the caller's record, so the assignment changes the caller's data.
let moveRightS (p: SPoint) =
let mutable copy = p
copy.SX <- copy.SX + 100
A struct parameter is a copy, and parameters are immutable, so we cannot even
assign to p.SX. We copy the parameter into a mutable local
binding and change that. The caller's struct stays as it was.
λ dotnet fsi parameters.fsx record after moveRight: (101, 2) inside moveRightS: (101, 2) struct after moveRightS: (1, 2)
Copy and update expressions
To get an independent copy of a record, we create a new one. The
copy and update expression { r with ... } builds a new
record from an existing one, replacing the listed fields.
type Point = { mutable X: int; mutable Y: int }
let p1 = { X = 1; Y = 2 }
let p2 = { p1 with X = p1.X }
let p3 = { p1 with Y = 30 }
p2.X <- 10
printfn "p1: (%d, %d)" p1.X p1.Y
printfn "p2: (%d, %d)" p2.X p2.Y
printfn "p3: (%d, %d)" p3.X p3.Y
printfn "p1 and p2 same object: %b" (obj.ReferenceEquals(p1, p2))
We make two new records from p1 and change one of them.
let p2 = { p1 with X = p1.X }
let p3 = { p1 with Y = 30 }
The expression needs at least one field after with; repeating a
field with its own value makes an exact copy. Both p2 and
p3 are new objects.
p2.X <- 10
Changing the copy does not affect the original, and
obj.ReferenceEquals confirms that they are different objects.
λ dotnet fsi copy_update.fsx p1: (1, 2) p2: (10, 2) p3: (1, 30) p1 and p2 same object: false
Sharing reference cells
A reference cell is a small object that holds a mutable value. Like a record,
it is a reference type, so copying a cell shares it. A mutable
binding of an integer, on the other hand, is copied.
let counter = ref 0 let alias = counter alias.Value <- alias.Value + 5 printfn "counter: %d" counter.Value printfn "alias: %d" alias.Value let mutable n = 0 let mutable m = n m <- m + 5 printfn "n: %d" n printfn "m: %d" m
The program compares a reference cell with a mutable integer binding.
let counter = ref 0 let alias = counter alias.Value <- alias.Value + 5
Both names refer to the same cell, so the new value is visible through
counter as well.
let mutable n = 0 let mutable m = n m <- m + 5
The mutable integer m starts as a copy of n;
changing it has no effect on n.
λ dotnet fsi ref_cells.fsx counter: 5 alias: 5 n: 0 m: 5
Shallow copies
A copy and update expression copies the fields of a record, but it does not copy the objects those fields refer to. Such a copy is called a shallow copy. When a field holds a reference, the original and the copy share the referenced object.
type Account = { Owner: string; Balance: int ref }
let acc1 = { Owner = "Roger Roe"; Balance = ref 100 }
let acc2 = { acc1 with Owner = "Paul Smith" }
acc2.Balance.Value <- 250
printfn "%s: %d" acc1.Owner acc1.Balance.Value
printfn "%s: %d" acc2.Owner acc2.Balance.Value
let acc3 = { acc1 with Owner = "Lucy Brown"; Balance = ref acc1.Balance.Value }
acc3.Balance.Value <- 500
printfn "%s: %d" acc1.Owner acc1.Balance.Value
printfn "%s: %d" acc3.Owner acc3.Balance.Value
An account stores its balance in a reference cell. We copy the account twice, once sharing the cell and once with a new cell.
let acc2 = { acc1 with Owner = "Paul Smith" }
acc2.Balance.Value <- 250
The new record gets its own Owner, but its Balance
field is a copy of the reference to the same cell. Changing the balance of
acc2 changes the balance of acc1 as well.
let acc3 = { acc1 with Owner = "Lucy Brown"; Balance = ref acc1.Balance.Value }
To make the copy independent, we give it a new cell with the current value.
This is a deep copy of the account: no mutable part is shared, so
the change made through acc3 stays in acc3.
λ dotnet fsi shallow_copy.fsx Roger Roe: 250 Paul Smith: 250 Roger Roe: 250 Lucy Brown: 500
Source
F# language reference: Records
F# language reference: Structs
F# language reference: Copy and Update Record Expressions
F# language reference: Reference Cells
In this article we have shown when F# copies data and when it copies a reference. Struct records are copied, while ordinary records, strings and reference cells are shared; arrays are reference types as well, see F# array. Records and copy and update expressions are covered in F# records, value types in F# struct types, and mutable data in F# mutability and F# reference cells.
Author
List all F# tutorials.