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-member-definitions }

Remarks

Records are immutable by default, so that there is no update to an existing record possible. 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 record 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 }

If you were to update only on field of that record you could use the copy and update record expression like the following:

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

See Also

Records

F# Language Reference