Records in depth
The records lesson defined one record and changed one field. This one puts a record inside another, updates the inner one without losing its other fields, shares a definition through a header file, and compares a record with a map when the shape of a value is the question.
A record inside a record
A field of a record may hold another record, and a pattern reads the inner one as easily as the outer. This module keeps a person and an address, and returns the address field by its own name:
nested.erl
-module(nested). -export([main/0, move/2]). -record(address, {city, country}). -record(person, {name, address = #address{}}). move(#person{address = Address} = Person, City) -> Person#person{address = Address#address{city = City}}. main() -> Ada = #person{name = "Ada", address = #address{city = "Paris", country = "France"}}, Rome = move(Ada, "Rome"), io:format("~s~n", [(Rome#person.address)#address.city]), io:format("~s~n", [(Rome#person.address)#address.country]).
erlc nested.erl
erl -noshell -eval 'nested:main(), halt().'
Rome France
The default of the address field is an empty #address{}, so a person built without an address still has one with undefined fields. The parentheses around Rome#person.address are required before the second dot: field access binds tighter than the record syntax would suggest, and the compiler reads the parentheses as the whole inner record.
Update a nested field
move/2 is the update: it reads the inner record, changes one field of it, and puts it back in the outer record. An update with a fresh #address{city = City} would lose the country, which is the mistake to avoid:
Person#person{address = #address{city = City}} replaces the whole address with a record whose only set field is the city, so country returns to its default. The version above carries the old inner record into the update with Address#address{city = City} and keeps every other field. A test on both fields catches the difference, which is why the program prints the country as well as the city.
A header shared by two modules
A record definition belongs to the module that uses it, so two modules that exchange the record repeat it. The usual answer is a header file with the -record attribute and an -include in each module. The file is person.hrl:
person.hrl
-record(person, {name, age = 0}).
One module reads the record through a pattern and the other builds one, and both include the same file, so the fields cannot drift apart:
reader.erl
-module(reader). -export([name/1]). -include("person.hrl"). name(#person{name = Name}) -> Name.
uses_header.erl
-module(uses_header). -export([main/0]). -include("person.hrl"). main() -> Person = #person{name = "Ada"}, io:format("~p~n", [Person#person.name]).
erlc reader.erl uses_header.erl
erl -noshell -eval 'uses_header:main(), halt().'
"Ada"
-include("person.hrl") searches the folder of the module, and -include_lib("app/include/file.hrl") searches an installed OTP application instead. A module that includes a header twice defines the record twice and the compiler refuses it, so a header often guards its content with -ifndef, which the macros lesson shows.
Record or map
A record and a map hold the same idea: named fields. The difference is when the names
exist. A record's fields are fixed in the source, the compiler checks every read, and
the value is a tuple with the tag at position one. A map's keys exist at run time, a
read may return a default, and the set of keys can change while the program runs.
The rule of thumb: a record for the fixed shape of a message between two modules of one
program, a map for JSON, configuration and anything whose keys a reader supplies. A
record with an atom tag is also what OTP itself returns: a file's metadata, a socket
option and a process's state are all records defined in an application's headers, which
the maps in depth lesson compares
with a map used as a record.
The OTP headers this package lacks
A record defined by OTP lives in an application header, for example
file_info in kernel/include/file.hrl.
The Alpine OTP package this course runs on ships the runtime and the beams but not the
include folders: there is no
kernel/include/file.hrl and no
eunit/include/eunit.hrl. A lesson here cannot
-include_lib an OTP header, so this page uses a header of its
own, and the EUnit lesson writes test
functions without the header.
On a full OTP install the same code compiles as written, and
file:read_file_info/1 returns an
#file_info{} whose size field is the
byte count. That call and its record are described here, not run, because the header
that names the fields is not in this image.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a field of a record hold when the field is itself a record?
Answer
Another tuple with its own tag
The inner record is a tuple as well, so a person holds a tag and an address tuple.
-
Why does
Person#person{address = #address{city = City}}lose the country?Answer
The new address record is built from the defaults of its other fields
A fresh record starts from its defaults, so only the city is set and the country returns to
undefined. -
What does
-include("person.hrl")do?Answer
Inserts the header file at that point, so both modules share the record
The include is a compile-time text insertion, so the definition is identical in every module that includes it.
-
Which form includes a header from an installed OTP application?
Answer
-include_lib("kernel/include/file.hrl")-include_libresolves the path inside an application of the code path. -
When is a map the better choice than a record?
Answer
When the keys are supplied while the program runs
A record needs its field names at compile time; a map takes keys from the running program.
-
Why can this course not run a program that uses
#file_info{}?Answer
The package has no kernel include folder, so the header cannot be included
The Alpine OTP package ships the runtime without the include folders, so an OTP header cannot be included here.
-
What does the parentheses around
Rome#person.addressgive before the second dot?Answer
The inner record as a value that the next field access reads
The parentheses group the inner record, so the following field access applies to it.
-
What does this module print?
-module(q1). -export([main/0]). -record(inner, {value = 1}). -record(outer, {inner = #inner{}}). main() -> O = #outer{}, io:format("~p~n", [(O#outer.inner)#inner.value]).Answer
1The default of the field is an
#inner{}, whose own default value is 1. -
What does this module print?
-module(q2). -export([main/0]). -record(point, {x = 0, y = 0}). move(#point{x = X, y = Y}) -> #point{x = X + 1, y = Y}. main() -> P = move(#point{x = 1, y = 2}), io:format("~p~n", [{P#point.x, P#point.y}]).Answer
{2,2}The update adds one to x and keeps y at 2.
-
What does this module print?
-module(q3). -export([main/0]). -record(person, {name, age = 0}). main() -> P = #person{name = "Ada"}, Q = P#person{name = "Grace"}, io:format("~p~n", [{Q#person.name, Q#person.age}]).Answer
{"Grace",0}The update changes the name only, and the age keeps its default 0.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container. The OTP headers are not in that package, so the `#file_info{}` record is described and not run.
Article author: Arthur Isaev