English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Records (Record) in Erlang

Erlang has additional features for creating records. These records are composed of fields. For example, you can define a personal record that contains2fields, one is id, and the other is the name field. Then, in Erlang, you can create various instances of the record to define multiple people with different names and IDs.

Let's explore how to handle records.

Create record

To create a record, use a record identifier to specify the individual fields that make up the record. The following provides the general syntax and an example.

Syntax

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

Parameters

  • recordname −This is the name of the record.

  • Field1,Field2 ..Fieldn −These are the lists of individual fields that make up 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 a record with2a record definition with two fields, one is id, and the other is the name field. In addition, records are constructed in the following way-

Syntax

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

When defining a record instance, you can assign values to each field in it.

Access record value

To access the field and value of a specific record, use the following syntax.

Syntax

#recordname.Fieldname

Parameters

  • recordname −This is the name of the record.

  • Fieldname −This is the name of the field 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"

Update record value

To update the value of a record, complete the record value update by assigning the value to a specific field and then assigning the record to a new variable name. The following provides the general syntax and an example.

Syntax

#recordname.Fieldname = newvalue

Parameters

  • recordname −This is the name of the record.

  • Fieldname −This is the name of the field to be accessed.

  • newvalue −This is the new value to be assigned to the field.

Return value

A new record with a new value assigned to the field.

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 feature of nested records. The following example explains how to create these nested records.

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 points should be noted-

  • We first create a record for a person, with the field values of name and address.

  • Then we define an employee record, where person is a field, and there is also a field named id.

Output

The output of the above program is as follows.

1