Copy-and-update syntax creates a new F# record from an existing value while changing selected fields. It is the usual answer when application data must appear to change over time but record fields remain immutable: the original value stays safe to share, and the changed state is represented by a new value. section 5.3.2 section 5.7
Write the source record before `with`, then list only the replacement fields. Unlisted fields are copied into the new record, so the code avoids manually rebuilding every field and makes the intended change explicit. section 5.3.2
Copy an address and change its location
Given `theAddress`, create `addressInDE` with `{ theAddress with Town = "Berlin"; Country = "DE" }`. The result has the changed town and country, while `theAddress` still represents the London, UK address. section 5.3.2
This is not field mutation. Using `<-` on an immutable record field produces a compiler error; copy-and-update instead records each change in a fresh value. A field can be declared `mutable` when necessary, such as for a record changed thousands of times per second in a tight loop, but immutable records are the recommended starting point until performance testing identifies a problem. section 5.3.2
Anonymous records extend the pattern: copy-and-update can override existing fields and add new ones at the same time. They suit exploration, scripts, one-off nested data, and mapping domain data into a JSON-shaped value, though they do not support inferred function-input types, attributes, or pattern matching. section 5.4.1
Listing 5.5 Using copy-and-update syntax to clone a record
let theAddress = #1
{
Line1 = "1st Street"
Line2 = "Apt. 1"
Town = "London"
Country = "UK"
}
let addressInDE = #2
{
theAddress with #3
Town = "Berlin" #4
Country = "DE" #4
}
Inspect a complete record literal and the compact copy that overrides only `Town` and `Country`. section 5.3.2
let companyWithBankDetails =
{|
company with
AccountNumber = 123
SortCode = 456
|}
See an anonymous record copied from `company` while adding bank-detail fields. section 5.4.1
