Erlang – Records

Erlang records

In this guide, we will discuss Erlang Records. Erlang has the extra facility to create records. These records consist of fields. For example, you can define a personal record which has 2 fields, one is the id and the other is the name field. In Erlang, you can then create various instances of this record to define multiple people with various names and id’s.

Let’s explore how we can work with records.

Creating a Record

A record is created using the Record Identifier. In this record identifier, you specify the various fields which constitute the record. The general syntax and example are given below.

Syntax

record(recordname , {Field1,Field2 ..Fieldn})

Parameters

  • recordname − This is the name given to the record.
  • Field1,Field2 ..Fieldn − These are the list of various fields which constitute the record.

Return Value

None

For example

-module(helloworld). 
-export([start/0]). 
-record(person, {name = "", id}). 

start() -> 
   P = #person{name="John",id = 1}.

The above example shows the definition of a record with 2 fields, one is the id and the other is the name. Also, a record is constructed in the following way −

Syntax

#recordname {fieldName1 = value1, fieldName2 = value2 .. fieldNameN = valueN}

Where in you assign values to the respective fields when an instance of the record is defined.

Accessing a Value of the Record

To access the fields and values of a particular record, the following syntax should be used.

Syntax

#recordname.Fieldname

Parameters

  • recordname − This is the name given to the record.
  • Fieldname − This is the name of the field which needs to be accessed.

Return Value

The value assigned to the field.

For example

-module(helloworld). 
-export([start/0]). 
-record(person, {name = "", id}). 

start() -> 
   P = #person{name = "John",id = 1}, 
   io:fwrite("~p~n",[P#person.id]), 
   io:fwrite("~p~n",[P#person.name]).

Output

The output of the above program is as follows.

1
“John”

Updating a Value of the Record

The updation of a record value is done by changing the value to a particular field and then assigning the record to a new variable name. The general syntax and example is given below.

Syntax

#recordname.Fieldname = newvalue

Parameters

  • recordname − This is the name given to the record.
  • Fieldname − This is the name of the field which needs to be accessed.
  • newvalue − This is the new value which needs to be assigned to the field.

Return Value

The new record with the new values assigned to the fields.

For example

-module(helloworld). 
-export([start/0]). 
-record(person, {name = "", id}). 

start() -> 
   P = #person{name = "John",id = 1}, 
   P1 = P#person{name = "Dan"}, 
   
   io:fwrite("~p~n",[P1#person.id]), 
   io:fwrite("~p~n",[P1#person.name]).

Output

The output of the above program is as follows −

1
“Dan”

Nested Records

Erlang also has the facility to have nested records. The following example shows how these nested records can be created.

For example

-module(helloworld). 
-export([start/0]). 
-record(person, {name = "", address}). 
-record(employee, {person, id}). 

start() -> 
   P = #employee{person = #person{name = "John",address = "A"},id = 1}, 
   io:fwrite("~p~n",[P#employee.id]).

In the above example the following things need to be noted −

  • We are first creating a person’s record which has the field values of name and address.
  • We then define an employee record which has the person as a field and an additional field called id.

Output

The output of the above program is as follows.

1

Next Topic : Click Here

This Post Has One Comment

Leave a Reply