Records represent simple aggregates of named values, optionally with members. They can either be structs or reference types. They are reference types by default.
[ attributes ]
type [accessibility-modifier] typename =
{ [ mutable ] label1 : type1;
[ mutable ] label2 : type2;
... }
[ member-list ]
[<Struct>]
attribute to create a struct record rather than a record that is a reference type.The following example defines a record containing Labels defined on the same line separated by semicolons.
type Point3D = { X: float; Y: float; Z: float; }
You can define labels on separate lines with or without a semicolon.
type Author =
{ FirstName: string
LastName: string;
Age: uint32 }
When each label is on a separate line, the semicolon is optional. The following code shows how to define a struct record.
[<Struct>]
type StructPoint =
{ X: float
Y: float
Z: float }
You can initialize records by using the labels that are defined in the record.
The following example shows how to create a record.
type Point3D = { X: float; Y: float; Z: float; }
let point = { X = 1.1; Y = 3.3; Z = 5.5; }
Console.WriteLine(point)
You can also specify the type name explicitly.
let point1 = { Point3D.X = 2.2; Point3D.Y = 4.4; Point3D.Z = 6.6; }
You can copy an existing record and also change some of the field values as shown below.
let author1 = {FirstName = "Mark"; LastName = "Upston"; Age = 33u;}
let author2 = { author1 with Age = 43u}
This form of the record expression is called the copy and update record expression.