Reader Level:
ARTICLE

Working with Records in FSharp

Posted by A K Articles | F# October 17, 2011
Record is used to group different types of data into a single unit in a structural format. Here you will learn about Records in FSharp.
  • 0
  • 0
  • 11306

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;
--------------;
--------------;
}

Like,

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

or,

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


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

let
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

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

let
(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

type
record1 = { x : int; y : int; z : int;}
let ref = { x=1; y=2;z=3}
let
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
type
student = { Name : string; City: string;}
let
student = {Name = "Satish";City= "Delhi" }

//accessing with record name

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


//instance
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

type
teacher = {Name : string; City: string;}
let
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 ""

Output:

accessing values in f#

COMMENT USING

Trending up