Records
A record is a tuple whose positions have names. The compiler reads the definition and turns every field access into a tuple index, so a record costs nothing at run time and a field name costs nothing but a compile step. It is the structure Erlang uses for a value whose shape is fixed in the source.
Define a record
The -record attribute at the top of the module names the record and its fields in order. A field may have a default after the equals sign; a field without one defaults to the atom undefined. The definition is compile-time only, and two modules that share a record repeat the attribute or include it from one header:
person.erl
-module(person). -export([main/0, new/2, age/1, birthday/1]). -record(person, {name, age = 0}). new(Name, Age) -> #person{name = Name, age = Age}. age(#person{age = Age}) -> Age. birthday(#person{age = Age} = Person) -> Person#person{age = Age + 1}. main() -> Ada = new("Ada", 36), Older = birthday(Ada), io:format("~p~n", [age(Ada)]), io:format("~p~n", [age(Older)]).
erlc person.erl
erl -noshell -eval 'person:main(), halt().'
36 37
Defaults and creation
A record expression lists the fields to set; the rest take their defaults. The tag is the first item of the tuple, so the value prints with the record syntax only in a shell that has the definition:
erl
1> rd(person, {name, age = 0}). person 2> #person{name = "Ada", age = 36}. #person{name = "Ada",age = 36} 3> #person{name = "Grace"}. #person{name = "Grace",age = 0} 4> #person{}. #person{name = undefined,age = 0}
The third line leaves age out and gets the default zero, and the fourth leaves both out and gets undefined for the field without a default. A field named in the expression is set; a field omitted takes its default, not the value of the previous record.
Read a field
The dot after the record reads one field, and a pattern with the record and the field reads it in a function head. The pattern also demands the tag, so a value of another record does not fit:
erl
1> rd(person, {name, age = 0}). person 2> P = #person{name = "Ada", age = 36}. #person{name = "Ada",age = 36} 3> P#person.name. "Ada" 4> P#person.age. 36 5> #person{name = Name} = P. #person{name = "Ada",age = 36} 6> Name. "Ada"
person:age/1 uses the pattern form, so the tag is checked before the field is read. A function that reads Value#person.age without a pattern raises a badrecord error instead, and the error carries the value that was not a person.
Update returns a new record
The field syntax on the left of an update returns a new record with one field changed. The old record keeps its value, as every term does. This module counts with a record and an update in a recursive loop:
counter.erl
-module(counter). -export([main/0, bump/1, run/2]). -record(counter, {name, value = 0}). bump(#counter{value = Value} = Counter) -> Counter#counter{value = Value + 1}. run(Counter, 0) -> Counter; run(Counter, Times) -> run(bump(Counter), Times - 1). main() -> Counter = run(#counter{name = "hits"}, 3), io:format("~p~n", [Counter#counter.value]).
erlc counter.erl
erl -noshell -eval 'counter:main(), halt().'
3
bump/1 returns a new record and run/2 passes it to the next call, so the count is a value that travels through the loop. The recursion lesson shows the same accumulator shape without a record.
The shell and rd
The shell has no compiler, so it does not read a -record attribute from a module. rd(Name, {Field, ...}) defines a record for the session, and rr(Module) reads the definitions out of a compiled beam. Both were used above; without either, a record expression fails:
erl
1> #account{owner = "Ada"}. * 1:1: record account undefined
The error is a compile error of the shell query, not a run-time one, and it names the record. The shell lesson shows rr/1 reading a beam.
What the compiler checks
The field must exist in the definition: a name that is not there is a compile error, not
a run-time lookup. field nickname undefined in record person
is the line and the message for a field that was never declared, and the caret points
at the field. The compiler also catches a record used without its definition in the
module; an update of a value that is another record fails at run time with
badrecord, not at compile time.
A value of another record reaches the function as a tuple, so a pattern with the record
tag is the check at run time; the
Records in depth lesson compares a
record with a map for a value whose shape changes.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this module print?
-module(q3). -export([main/0]). -record(user, {name, age = 18}). main() -> User = #user{name = "Ada"}, io:format("~p~n", [User#user.age]).Answer
18The field is omitted, so it takes its default of 18.
-
What is the default value of a field declared without one?
Answer
undefinedA field with no default is
undefined; a default after the equals sign replaces it. -
What does an update such as
P#person{age = 37}change?Answer
Nothing: it returns a new record and the old one keeps its value
Every term is immutable, so the update builds a new tuple with one position replaced.
-
How does the shell learn a record definition?
Answer
rd/2defines it, orrr/1reads it from a beamThe shell has no module to compile, so
rdorrrsupplies the field names. -
What does
#person{name = "Ada"}print in a shell with the record defined as{name, age = 0}?Answer
#person{name = "Ada",age = 0}The shell prints the record by its field names, and the omitted field takes its default.
-
A pattern
#person{}meets a value of another record. What happens?Answer
The clause does not match, so the call fails with
function_clauseA record pattern demands the tag, and a different record is a different tuple.
-
What does this module print?
-module(q4). -export([main/0]). -record(pair, {left, right}). swap(#pair{left = Left, right = Right}) -> #pair{left = Right, right = Left}. main() -> #pair{left = A, right = B} = swap(#pair{left = 1, right = 2}), io:format("~p~n", [{A, B}]).Answer
{2,1}The swap returns a new record with the fields exchanged, and the pattern reads them into A and B.
-
What does this module print?
-module(q1). -export([main/0]). -record(point, {x = 0, y = 0}). main() -> P = #point{x = 2}, io:format("~p~n", [P#point.x + P#point.y]).Answer
2The omitted field takes its default 0, so the sum is 2 + 0.
-
What does this module print?
-module(q2). -export([main/0]). -record(counter, {value = 0}). bump(Counter = #counter{value = Value}) -> Counter#counter{value = Value + 1}. main() -> First = bump(#counter{}), io:format("~p~n", [First#counter.value]).Answer
1The default value is 0, and the update stores 0 + 1.
-
A module reads
P#person.nicknamebut the record declaresnameandage. What does the compiler say?Answer
field nickname undefined in record personThe compiler knows the fields of the record, so a name outside the definition stops the build.
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.
Article author: Arthur Isaev