Records
Define a record
A record is a tuple with named positions. Define it at the top of the module with the record attribute, listing the field names in order.
The compiler turns record syntax into plain tuple access, so records cost nothing at runtime.
-module(person).
-export([new/2, age/1]).
-record(person, {name, age}).
new(Name, Age) ->
#person{name = Name, age = Age}.
age(P) ->
P#person.age.
Create one
The hash and record name build a record. Every field can take a value, and any field left out gets the atom undefined as its default.
In the shell the record is defined with rd, which takes the name and the field tuple. Set only the fields you care about, and let the defaults fill the rest.
erlc person.erl
erl
1> rd(person, {name, age}).
person
2> P = person:new("Ada", 36).
#person{name = "Ada",age = 36}
3> person:age(P).
36
4> #person{name = "Grace"}.
#person{name = "Grace",age = undefined}
Read fields
The dot syntax reads one field from a record. It is shorthand for element at the field position, with the tag checked for you.
The compiler raises a badarg error when the value is not the record you expect, which catches mix-ups early.
erl
1> rd(person, {name, age}).
person
2> P = #person{name = "Ada", age = 36}.
#person{name = "Ada",age = 36}
3> P#person.name.
"Ada"
4> P#person.age.
36
Update fields
The record expression with new values returns a fresh record with those fields replaced. The original stays untouched, as single assignment demands.
Update one field or many in the same expression, and chain the result straight into the next call.
erl
1> rd(person, {name, age}).
person
2> P = #person{name = "Ada", age = 36}.
#person{name = "Ada",age = 36}
3> P#person{age = 37}.
#person{name = "Ada",age = 37}
4> Older = P#person{age = P#person.age + 1}.
#person{name = "Ada",age = 37}
Next: Exceptions: try and catch
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a record at runtime?
Answer
A tuple with named positions
The compiler turns record syntax into tuple access, with the record name as the tag.
-
How is a record defined in a module?
Answer
With
-record(person, {name, age}).The
-recordattribute lists the record name and its fields in order. -
How is a record defined in the interactive shell?
Answer
rd(person, {name, age})rd/2adds the record definition to the shell evaluator. -
What does
#person{name = "Ada", age = 36}.build?Answer
A record with the given fields
The hash and record name build the record, filling the named fields.
-
What value does a field get when it is left out?
Answer
undefinedUnset fields default to the atom
undefined, as#person{name = "Grace"}shows. -
What does
P#person.name.read?Answer
The
namefieldThe dot syntax reads one named field from the record.
-
What does
P#person{age = 37}.return?Answer
A fresh record with
agereplacedThe expression builds a new record; the original binding stays untouched.
-
What is the tag of a
personrecord?Answer
personThe record name is the first element of the hidden tuple and its tag.
-
What does a record cost at runtime?
Answer
Nothing beyond the tuple access
Record syntax compiles to tuple element access, so there is no extra runtime structure.
-
What does
person:new("Ada", 36)return withrd(person, {name, age}).loaded?Answer
#person{name = "Ada",age = 36}With the record definition loaded, the shell prints the tuple using record syntax.
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