Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ To update only two fields in that record you can use the *copy and update record

[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet1906.fs)]

## Nested Record Copy and Update

In F# 7.0 and later, the *copy and update expression* has been enhanced to support updates on nested record fields. This feature allows for more concise syntax when working with deeply nested records.

Consider the following example:

### Before

[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet19061.fs)]

### After

With the new feature, you can use dot-notation to reach nested fields and update them directly:

[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet19062.fs)]

This syntax eliminates the need for multiple `with` expressions. Instead, it allows for specifying updates on nested fields directly, while still allowing multiple fields (even at different levels of nesting) to be updated in the same expression.

### Anonymous Records

The same syntax extension works for anonymous records as well. Additionally, you can use this syntax to copy and update regular records into anonymous ones, adding new fields in the process:

[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet19063.fs)]

This flexibility ensures that the same concise syntax applies whether you're working with regular or anonymous records.

## See also

- [Records](records.md)
Expand Down
10 changes: 10 additions & 0 deletions samples/snippets/fsharp/lang-ref-1/snippet19061.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type SteeringWheel = { Type: string }
type CarInterior = { Steering: SteeringWheel; Seats: int }
type Car = { Interior: CarInterior; ExteriorColor: string option }

let beforeThisFeature x =
{ x with Interior = { x.Interior with
Steering = {x.Interior.Steering with Type = "yoke"}
Seats = 5
}
}
2 changes: 2 additions & 0 deletions samples/snippets/fsharp/lang-ref-1/snippet19062.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let withTheFeature x =
{ x with Interior.Steering.Type = "yoke"; Interior.Seats = 5 }
4 changes: 4 additions & 0 deletions samples/snippets/fsharp/lang-ref-1/snippet19063.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let updatedRecord =
{| originalRecord with
Interior.Seats = 4;
Price = 35000 |}
Loading