Muokkaa

Copy and Update Record Expressions

A copy and update record expression is an expression that copies an existing record, updates specified fields, and returns the updated record.

Syntax

{ record-name with
    updated-labels }

{| anonymous-record-name with
    updated-labels |}

Remarks

Records and anonymous records are immutable by default, so it is not possible to update an existing record. To create an updated record all the fields of a record would have to be specified again. To simplify this task a copy and update expression can be used. This expression takes an existing record, creates a new one of the same type by using specified fields from the expression and the missing field specified by the expression.

This can be useful when you have to copy an existing record, and possibly change some of the field values.

Take for instance a newly created record.

let myRecord2 =
    { MyRecord.X = 1
      MyRecord.Y = 2
      MyRecord.Z = 3 }

To update only two fields in that record you can use the copy and update record expression:

let myRecord3 = { myRecord2 with Y = 100; Z = 2 }

See also