Understanding F# Records

The purpose of this article is to illustrate how F# records can be defined and used. Here, we are going to talk about the syntax that is required for defining records, accessing their fields, and creating instances of records.

Moreover, we will talk about the advantages of using F# records, such as the fact that they are immutable and that they support pattern matching, as well.



F# Records

F# record is similar to a class or struct in another programming language, but there are a few key differences that make them special from other programming languages.

Record values are immutable by default, which means that once they have been created, they cannot be changed.

A record type can be considered as another value type, so it is similar to a reference type in that it is passed as a value instead of a reference to another record.

Syntax

type website =
{ title : string;
url : string }

Create a F# Record

In order to create a record, you need to specify the fields of the record.

Suppose that we were to create a record for the website called home, for example:

let homepage = { Title = "mrexamples"; Url = "www.mrexamples.com" }

Below is an example that clearly illustrates the concept of records.


Declare Record In F#

There are two types of records defined, namely:

  • Records that are defined by the type keyword.
  • Records that contain fields.

Both are defined by a semicolon-separated list of fields.

The syntax for defining a record is as follows:

type recordName =
{ [ fieldName : dataType ] + }

 

Defining A Record Type Named Website

Below is an example of defining records of the website name and their urls:

Example: 

type website = { Title : string; Url : string }//creating records let home = { Title = "mrexample"; Url = "https://mrexamples.com/" } let fsharp = { Title = "Learn F#"; Url = "https://mrexamples.com/f-sharp/" } let pythonTutorial = { Title = "Learn Python"; Url = "https://mrexamples.com/python/" } let phpTutorial = { Title = "Learn PHP"; Url = "https://mrexamples.com/php/" }//printing records (printfn "Home: Title: %A \nURL: %A") home.Title home.Url (printfn "\nLearn F# from mrexamples: Title: %A \nURL: %A") fsharp.Title fsharp.Url (printfn "\nLearn Python from mrexamples: Title: %A \nURL: %A") pythonTutorial.Title pythonTutorial.Url (printfn "\nLearn Python from mrexamples: Title: %A \nURL: %A") phpTutorial.Title phpTutorial.Url

In the example above, the following records are printed:

Home: Title: mrexample
URL: https://mrexamples.com/

Learn F# from mrexamples: Title: Learn F#
URL: https://mrexamples.com/f-sharp/

Learn Python from mrexamples: Title: Learn Python
URL: https://mrexamples.com/python/

Learn Python from mrexamples: Title: Learn PHP
URL: https://mrexamples.com/php/

Another example of creating a record about a pet:

Example: 

type Pet = { Name: string Age: int Species: string Owner: string } let myPet = { Name = "Cute"; Age = 2; Species = "Dog"; Owner = "Christopher" }printfn "My pet's name is %s" myPet.Name printfn "My pet's age is %d" myPet.Agelet printPetAge pet = match pet with | { Age = age } -> printfn "The pet's age is %d" age | _ -> printfn "Unable to determine pet's age"printPetAge myPet

The output will be as:

F# Record examples

Example Explanation

In above example we have define a record type named Pet with four fields: Name of type string, Age of type int, Species of type string, and Owner of type string.

Then we creates an instance of the Pet record named myPet with the values “Cute” for the name, 2 for the age, “Dog” for the species, and “Christopher” for the owner.

Next, we used the printfn function to print out the name and age of myPet record to the console.

Finally, we define a function named printPetAge that takes a Pet record as input and uses pattern matching to extract the Age field and print it to the console. If the Age field is not present in the input record, the function prints “Unable to determine pet’s age“.

The printPetAge function is called with myPet record as an argument, and it prints The pet’s age is 2 to the console.


Student Registration Records

Below is an example of registering student record:

Example: 

type student = { Name : string; ID : int; Registration : string; IsRegistered : bool }let getStudent name id = { Name = name; ID = id; Registration = null; IsRegistered = false }let studentRegistration stu = { stu with Registration = "Registered"; IsRegistered = true }let printStudent msg stu = printfn "%s: %A" msg stulet main() = let preRegistered = getStudent "Jennifer" 105 let postRegistered = studentRegisteration preRegisteredprintStudent "Before Registration: " preRegistered printStudent "\nAfter Registration: " postRegisteredmain()

The results of both before and after registration and will be displayed as follows:

Before Registration: : {Name = Jennifer;
ID = 105;
Registration = null;
IsRegistered = false}

After Registration: : {Name = Jennifer;
ID = 105;
Registration = Registered;
IsRegistered = true}
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *