Working with Records in FSharp

Introduction: Record is used to group different types of data into a single unit in structural format. Records are immutable by default. It is similar to structure in C language. Its values are separated by semicolons. A record definition starts with the type keyword then we simply define a series of data in the format of Label : Type in curly braces. The syntax for Record definition is given below.

Defining a Record

type record_type_name={
Label1 : type1;
Label2 : type2;


type Rectyp = { First : string; Last : string; City: string; Age : int}


type Rectyp = {
First : string;
Last : string;
City: string;
Age : int

Here RecTyp is the Record name and First, Last, City and Age are Lalels and string and int are their Datatypes.

Creating an Instance:  Now we create a record with named student and fill values First_Name, Last_Name, City and Age. We write the following code.

// Creating Record

student = { First_Name : string; Last_Name : string; City: string; Age : int}

ob = {
  First_Name = "Satish";
  Last_Name = "Kumar";
  City= "Delhi";
  Age = 14

printfn " First Name %s" ob.First_Name
printfn " Last Name %s" ob.Last_Name
printfn " City %s" ob.City
printfn " Age %d" ob.Age 

Now we run this application by pressing Crtl+F5 key. The output will look like the below figure

record in f#

It may be that the field name of two or more records is the same. In that case, the compiler requires information about the types of records to be operated. For that, we need to create an instance of record. We create an instances with a Colon (:), like the below example.

// Creating Record's instance

student = { Name : string; City: string;}
employee={ Name : string; Department: string;}

(std : student) = {
  Name = "Satish";
  City= "Delhi"
let (emp : employee) = {Name = "Alok"; Department = "IT"}

Copy of a Record: Records can be easily copied into another record with updated value. To copy a record, we use the with keyword between the old record name and a list of updated fields in curly braces. To understand it, we write the following code

// Copy and updating Record

record1 = { x : int; y : int; z : int;}
let ref = { x=1; y=2;z=3}
cpy={ ref with z=4}

printfn "%A" ref
printfn "%A" cpy

Now we run this code. The output will look like the below figure

copy of record in f#

Accessing Value: The field values are accessed by the dot (.) operator. We use the  dot (.) operator with record_name or instance name of a record, like the following example,

//simple record
student = { Name : string; City: string;}
student = {Name = "Satish";City= "Delhi" }

//accessing with record name

printfn "Name :%s" student.Name
printfn "City :%s" student.City

type employee = {Name : string; City: string;}
let (emp : employee) = {Name = "Satish";City= "Delhi" }

//accessing with instance name

printfn "\nName :%s" emp.Name
printfn "City :%s" emp.City 

//specifying the type

teacher = {Name : string; City: string;}
teacher = {teacher.Name = "Satish";City= "Delhi" }
printfn "\nName :%s" teacher.Name 
printfn "City :%s" teacher.City  

//All field value

printfn "\nAll field values of teacher record \n %A " teacher
printfn ""


accessing values in f#