![]() |
Records |
|
Fundamentals of Records |
|
Introduction |
|
In Lesson 1, we saw how to declare individual variables to store values in them. A record is a group of variables that are considered as one entity. That is, inside the record, you can create (declare) one or more variables and the variables can be of different types. Then, when you need the services of the record, you can either access the record as a whole or access its members individually. |
|
Creating a Record |
|
The basic formula to create a record is: type RecordName = { Members }
You start with the type keyword, followed by a name. The name of the record follows the rules of name of the F# language. Unlike a variable, you should start the name with a letter in uppercase. After the name, type = followed an opening and a closing curly brackets. Inside the brackets, enter the name of each member; they must be separated by semicolons. Each member must use the following formula: MemberName : DataType That is, each member must be created with a name and its data type. A record can have just one member. Here is an example: type Student = { FullName : string }
Otherwise, a record can have as many members as you want. Here is an example: type Student = { ID : int; FirstName : string; LastName : string; Gender : char }
If there are too many members or to make the code easier to read, you can list the members on different lines, or each on its own line. Here is an example: type Student = {
ID : int;
FirstName : string;
LastName : string;
Gender : char
}
If you use this technique, you can omit the semicolons. Here is an example: type Student = {
ID : int
FirstName : string
LastName : string
Gender : char
}
Just like you can create one record in a file, you can created as many records as you want in the same file.
Before using a record, you should assign a value to each of its members. You do this by creating a record expression. To create a record expression, use the following formula: let VariableName = { MemberName1 = Value1; MemberName_n = Value_n }
As always, start with let followed by a name for the variable and assign curly brackets to it. Inside the brackets, enter each member of the record and assign a value to it; separate them with semicolons. Here is an example: type Student = {
ID : int
FirstName : string
LastName : string
Gender : char
}
let std = { ID = 773946; FirstName = "Amir";
LastName = "Shalloub"; Gender = 'M' }
Remember that you can write each member on its own line to make the code easier to read, in which case you can omit the semicolons: type Student = {
ID : int
FirstName : string
LastName : string
Gender : char
}
let std = {
ID = 773946
FirstName = "Amir"
LastName = "Shalloub"
Gender = 'M'
}
Notice that the name of the class is not specified in the declaration, but the compiler must be able to identify or recognize the members that are being accessed in the curly brackets of the variable. This also means that the record must be created before the variable that initializes it. Since the variable doesn't identify the record by name, what if there are two or more records that have the same members? Consider the following example: type Student = {
ID : int
FirstName : string
LastName : string
Gender : char
}
type Employee = {
ID : int
FirstName : string
LastName : string
Gender : char
}
let std = {
ID = 773946
FirstName = "Amir"
LastName = "Shalloub"
Gender = 'M'
}
Notice that both the Student and the Employee records have the exact same members. This situation qualifies as ambiguous (common in functional and logical languages). In this case, when you declare a variable that initializes a record, the variable will use the record that is closest to it, which is the most recently created record. In our case, this would be the Employee record. If you have different records that use the same name, when declaring a variable that initializes them, you can qualify one or each member in the curcly brackets of the variable. To do this, type the name of the record, followed by a period, and followed by the name of the member. Then assign the appropriate value to the member. Here are examples: type Student = {
ID : int
FirstName : string
LastName : string
Gender : char
}
type Employee = {
ID : int
FirstName : string
LastName : string
Gender : char
}
let std = {
Student.ID = 773946
Student.FirstName = "Amir"
Student.LastName = "Shalloub"
Student.Gender = 'M'
}
let contractor = {
Employee.ID = 50225
Employee.FirstName = "Hellie"
Employee.LastName = "Pastore"
Employee.Gender = 'F'
}
After initializing a record, one way you can use it consists of presenting its values to the user. To do this, use the printfn() function and specify %A as the value placeholder. Here is an example: type Country = {
Continent: string
Name : string
Capital : string
Area : uint32
InternetCode: string
}
let Ecuador = {
Continent = "South America"
Name = "Ecuador"
Capital = "Quito"
Area = 283561u
InternetCode = "ec"
}
printfn "%A" Ecuador
This would produce: {Continent = "South America";
Name = "Ecuador";
Capital = "Quito";
Area = 283561u;
InternetCode = "ec";}
Press any key to continue . . .
To access a member of a record, type the name of the variable you declared, followed by a period, followed by the member you want. Here are examples: type Student = {
ID : int
FirstName : string
LastName : string
Gender : char
}
let std = {
ID = 773946
FirstName = "Amir"
LastName = "Shalloub"
Gender = 'M'
}
printfn "Student Information";
printfn "Student ID: %d" std.ID;
printfn "First Name: %s" std.FirstName;
printfn "Last Name: %s" std.LastName;
printfn "Gender: %c" std.Gender;
This would produce: Student Information Student ID: 773946 First Name: Amir Last Name: Shalloub Gender: M Press any key to continue . . . Remember that, if you are using two or more records that have the same members, to identify a member, you should qualify it in the curly brackets of the variable. Here are examples: type Student = {
ID : int
FirstName : string
LastName : string
Gender : char
}
type Employee = {
ID : int
FirstName : string
LastName : string
Gender : char
}
let std = {
Student.ID = 773946
FirstName = "Amir"
LastName = "Shalloub"
Gender = 'M'
}
let contractor = {
ID = 50225
Employee.FirstName = "Hellie"
LastName = "Pastore"
Gender = 'F'
}
printfn "Student Information";
printfn "Student ID: %d" std.ID;
printfn "First Name: %s" std.FirstName;
printfn "Last Name: %s" std.LastName;
printfn "Gender: %c" std.Gender;
printfn "=----------------------="
printfn "Employee Record";
printfn "Employee ID: %d" contractor.ID;
printfn "First Name: %s" contractor.FirstName;
printfn "Last Name: %s" contractor.LastName;
printfn "Gender: %c" contractor.Gender;
This would produce: Student Information Student ID: 773946 First Name: Amir Last Name: Shalloub Gender: M =----------------------= Employee Record Employee ID: 50225 First Name: Hellie Last Name: Pastore Gender: F Press any key to continue . . .
|
|
|
||
| Previous | Copyright © 2010-2011 FunctionX | Next |
|
|
||