1 Records
A record is a data structure intended for storing a fixed number of related data items. It is similar to a
struct
in C, or arecord
in Pascal.The main advantage of using records instead of tuples is that fields in a record are accessed by name, whereas fields in a tuple are accessed by position. To illustrate these differences, suppose that we want to represent a person with the tuple
{Name, Address, Phone}
.We must remember that the
Name
field is the first element of the tuple, theAddress
field is the second element, and so on, in order to write functions which manipulate this data. For example, to extract data from a variableP
which contains such a tuple we might write the following code and then use pattern matching to extract the relevant fields.Name = element(1, P), Address = element(2, P), ...Code like this is difficult to read and understand and errors occur if we get the numbering of the elements in the tuple wrong. If we change the data representation by re-ordering the fields, or by adding or removing a field, then all references to the person tuple, wherever they occur, must be checked and possibly modified.
Records allow us to refer to the fields by name and not position. We use a record instead of a tuple to store the data . If we write a record definition of the type shown below, we can then refer to the fields of the record by name.
-record(person, {name, phone, address}).For example, if
P
is now a variable whose value is aperson
record, we can code as follows in order to access the name and address fields of the records.Name = P#person.name, Address = P#person.address, ...In the following sections we describe the different operations which can be performed on records:
1.1 Defining a Record
A record is defined with the following syntax:
-record(RecordName, {Field1 [= DefaultValue1], Field2 [= DefaultValue2], ..., FieldN [= DefaultValueN]}).The record name and field names must be atoms. The optional default values, which are terms, are used if no value is supplied for a field when a new instance of the record is created. If the default value is not supplied, then the atom
undefined
is assumed.For example, in the following record definition, the address field is
undefined
.-record(person, {name = "", phone = [], address}).This definition of a person will be used in many of the examples which follow.
1.2 Including a Record Definition
If the record is used in several modules, its definition should be placed in a
.hrl
header file. Each module which uses the record definition should have a-include(FileName).
statement. For example:-include("my_data_structures.hrl").
The definition of the record must come before it is used.
1.3 Creating a Record
A new record is created with the following syntax:
#RecordName{Field1=Expr1, ..., FieldM=ExprM}.If any of the fields is omitted, then the default value supplied in the record definition is used. For example:
> #person{phone = [0,8,2,3,4,3,1,2], name = "Robert"}. {person, "Robert", [0,8,2,3,4,3,1,2], undefined}.1.4 Selectors
The following syntax is used to select an individual field from a record:
Variable#RecordName.Field
The values contained in record names and fields must be constants, not variables.
For the purposes of illustration, we will demonstrate the use of records using an imaginary dialogue with the Erlang shell. Currently the Erlang evaluator does not support records so you may not be able to reproduce this dialogue.
> P = #person{name = "Joe", phone = [0,8,2,3,4,3,1,2]}. {person, "Joe", [0,8,2,3,4,3,1,2], undefined} > P#person.name. "Joe"
Selectors for records are allowed in guards.
1.5 Updating a Record
The following syntax is used to create a new copy of the record with some of the fields changed. Only the fields to be changed need to be referred to, all other fields retain their old values.
OldVariable#RecordName{Field1 = NewValue1, ..., FieldM = NewValueM}For example:
> P1 = #person{name="Joe", phone=[1,2,3], address="A street"}. {person, "Joe", [1,2,3], "A street"} > P2 = P1#person{name="Robert"}. {person, "Robert", [1,2,3], "A street"}1.6 Type Testing
The following guard test is used to test the type of a record:
record(Variable, RecordName)The following example shows that the guard succeeds if
P
is record of typeperson
.foo(P) when record(P, person) -> a_person; foo(_) -> not_a_person.
This test checks that
P
is a tuple of arityN + 1
, whereN
is the number of fields in the record, and the first element in the tuple is the atomperson
.1.7 Pattern Matching
Matching can be used in combination with records as shown in the following example:
> P = #person{name="Joe", phone=[0,0,7], address="A street"}. {person, "Joe", [0,0,7], "A street"} > #person{name = Name} = P, Name. "Joe"The following function takes a list of
person
records and searches for the phone number of a person with a particular name:find_phone([#person{name=Name, phone=Phone} | _], Name) -> {found, Phone}; find_phone([_| T], Name) -> find_phone(T, Name); find_phone([], Name) -> not_found.
The fields referred to in the pattern can be given in any order.
1.8 Nested Records
The value of a field in a record might be an instance of a record. Retrieval of nested data can be done stepwise, or in a single step, as shown in the following example:
-record(person, {name = #name{}, phone}). -record(name, {first = "Robert", last = "Ericsson"}). demo() -> P = #person{name= #name{first="Robert",last="Virding"}, phone=123}, First = (P#person.name)#name.first.
In this example,
demo()
evaluates to"Robert"
.1.9 Internal Representation of Records
It is often desirable to write generic functions which will work on any record, not just a record of a particular type. For this reason, records are represented internally as tuples and the ordering of the fields in the tuple is strictly defined.
For example, the record
-record(person, {name, phone, address}).
is represented internally by the tuple{person, X, Y, Z}
.The arity of the tuple is one more than the number of fields in the tuple. The first element of the tuple is the name of the record, and the elements of the tuple are the fields in the record. The variables
X
,Y
andZ
will store the data contained in the record fields.The following two functions determine the indices in the tuple which refer to the named fields in the record:
record_info(fields, Rec) -> [Names]
. This function returns the names of the fields in the recordRec
. For example,record_info(fields, person)
evaluates to[name, address, phone]
.
record_info(size, Rec) -> Size
. This function returns the size of the recordRec
when represented as a tuple, which is one more than the number of fields. For example,record_info(size, person)
returns4
.
In addition,
#Rec.Name
returns the index in the tuple representation ofName
of the recordRec
.
Name
must be an atom.For example, the following test function
test()
might return the result shown:test() -> {record_info(fields, person), record_info(size, person), #person.name}.> Mod:test(). {[name,address,phone],4,2}The order in which records map onto tuples is implementation dependent.
record_info
is a pseudo-function which cannot be exported from the module where it occurs.1.10 Example
%% File: person.hrl %%----------------------------------------------------------- %% Data Type: person %% where: %% name: A string (default is undefined). %% age: An integer (default is undefined). %% phone: A list of integers (default is []). %% dict: A dictionary containing various information %% about the person. %% A {Key, Value} list (default is the empty list). %%------------------------------------------------------------ -record(person, {name, age, phone = [], dict = []}).-module(person). -include("person.hrl"). -compile(export_all). % For test purposes only. %% This creates an instance of a person. %% Note: The phone number is not supplied so the %% default value [] will be used. make_hacker_without_phone(Name, Age) -> #person{name = Name, age = Age, dict = [{computer_knowledge, excellent}, {drinks, coke}]}. %% This demonstrates matching in arguments print(#person{name = Name, age = Age, phone = Phone, dict = Dict}) -> io:format("Name: ~s, Age: ~w, Phone: ~w ~n" "Dictionary: ~w.~n", [Name, Age, Phone, Dict]). %% Demonstrates type testing, selector, updating. birthday(P) when record(P, person) -> P#person{age = P#person.age + 1}. register_two_hackers() -> Hacker1 = make_hacker_without_phone("Joe", 29), OldHacker = birthday(Hacker1), % The central_register_server should have % an interface function for this. central_register_server ! {register_person, Hacker1}, central_register_server ! {register_person, OldHacker#person{name = "Robert", phone = [0,8,3,2,4,5,3,1]}}.