Skip to content

Commit cb06142

Browse files
shethaaditAdit Sheth
andauthored
Resolved Bug 43608. (#43667)
Co-authored-by: Adit Sheth <[email protected]>
1 parent cc20fc0 commit cb06142

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

docs/fsharp/language-reference/copy-and-update-record-expressions.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ To update only two fields in that record you can use the *copy and update record
3232

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

35+
## Nested Record Copy and Update
36+
37+
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.
38+
39+
Consider the following example:
40+
41+
### Before
42+
43+
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet19061.fs)]
44+
45+
### After
46+
47+
With the new feature, you can use dot-notation to reach nested fields and update them directly:
48+
49+
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet19062.fs)]
50+
51+
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.
52+
53+
### Anonymous Records
54+
55+
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:
56+
57+
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet19063.fs)]
58+
59+
This flexibility ensures that the same concise syntax applies whether you're working with regular or anonymous records.
60+
3561
## See also
3662

3763
- [Records](records.md)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type SteeringWheel = { Type: string }
2+
type CarInterior = { Steering: SteeringWheel; Seats: int }
3+
type Car = { Interior: CarInterior; ExteriorColor: string option }
4+
5+
let beforeThisFeature x =
6+
{ x with Interior = { x.Interior with
7+
Steering = {x.Interior.Steering with Type = "yoke"}
8+
Seats = 5
9+
}
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let withTheFeature x =
2+
{ x with Interior.Steering.Type = "yoke"; Interior.Seats = 5 }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let updatedRecord =
2+
{| originalRecord with
3+
Interior.Seats = 4;
4+
Price = 35000 |}

0 commit comments

Comments
 (0)