Keyword lists
A keyword list, or property list, is an ordinary list of pairs whose first element is an atom: [{port, 5432}, {timeout, 5000}]. It is not a special type, and that is its strength: every list function works on it, it keeps its order, and a key may appear twice. Erlang and OTP use this shape for function options, so a reader meets it in almost every library.
What keyword lists are
Each entry is a two-item tuple with an atom first. A bare atom is shorthand for {Atom, true}, which is why [verbose] and [{verbose, true}] mean the same option. The pair syntax in the shell is the same syntax a map literal uses, so the two are easy to confuse:
erl
1> [{name, "Ada"}, {age, 36}]. [{name,"Ada"},{age,36}] 2> [{tag, a}, {tag, b}]. [{tag,a},{tag,b}] 3> is_list([{name, "Ada"}]). true 4> is_map(#{name => "Ada"}). true
The first two values are lists and print in the order they were written. The third is a list too, and the fourth is a map, which prints with => and does not promise an order. A map is the subject of the maps lesson.
Read a value
The proplists module reads, deletes and replaces entries by key. get_value/2 answers the first value for a key or the atom undefined; get_value/3 takes a default instead. get_bool/2 answers a boolean and is_defined/2 answers whether the key is there at all:
erl
1> Opts = [{mode, fast}, {retries, 3}]. [{mode,fast},{retries,3}] 2> proplists:get_value(mode, Opts). fast 3> proplists:get_value(timeout, Opts, 5000). 5000 4> proplists:get_bool(verbose, [verbose]). true 5> opts:connect("db.local", [{timeout, 100}]). {connect,"db.local",5432,100}
A missing key is not an error here, which is what makes a keyword list the friendly way to pass options: the caller names only what it wants to change. The price is a linear walk of the list for each lookup, so a keyword list is for a handful of options, not for a table of a thousand rows.
An options function
Most code builds a keyword list at the call site. This module reads the port and the timeout out of its options and fills in both defaults, so the caller writes only the port it wants:
opts.erl
-module(opts). -export([main/0, connect/2]). connect(Host, Opts) -> Port = proplists:get_value(port, Opts, 5432), Timeout = proplists:get_value(timeout, Opts, 5000), {connect, Host, Port, Timeout}. main() -> io:format("~p~n", [connect("db.local", [{port, 5433}])]), io:format("~p~n", [connect("db.local", [])]).
erlc opts.erl
erl -noshell -eval 'opts:main(), halt().'
{connect,"db.local",5433,5000} {connect,"db.local",5432,5000}
The same function with no options still returns a complete record of the settings, which is why the defaults live in the function and not at every call site. A library that grows more options keeps this shape and adds a key; the existing callers do not change.
Order and repeated keys
A list keeps every entry, in order. get_value/2 answers the first match and get_all_values/2 answers all of them, so a repeated key is not lost in silence: the program decides which one wins. This module holds three tags:
dup_opts.erl
-module(dup_opts). -export([main/0]). main() -> Opts = [{tag, normal}, {tag, alt}, {tag, normal}], io:format("~p~n", [proplists:get_value(tag, Opts)]), io:format("~p~n", [proplists:get_all_values(tag, Opts)]).
erlc dup_opts.erl
erl -noshell -eval 'dup_opts:main(), halt().'
normal [normal,alt,normal]
A map would have kept one tag and silently replaced the others. That difference is the reason OTP passes a list to a function such as gen_tcp:connect/4: an option may appear twice, and the first one wins on purpose.
Keyword list or map
The choice follows from the two properties. A keyword list wins when order matters, a
key may repeat, or the thing is a short list of options that the caller names one by
one; its lookup costs a walk. A map wins when the data is a table with one value per
key and lookups happen often. The mistake is to write a map where the caller expects a
keyword list: proplists:get_value/2 on a map raises a
function_clause, because a map is not a list at all.
A bare atom entry is the other trap. [verbose],
[{verbose, true}] and [{verbose, yes}]
are three different lists, and only the first two are a true flag to
get_bool/2: the third answers
false, because the value is neither the atom
true nor the bare atom shorthand. A reader who writes
yes has written an option that is quietly off.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a keyword list?
Answer
A list of two-item tuples whose first element is an atom
Each entry is a pair such as
{port, 5432}, and the whole thing is an ordinary list. -
Which property does a keyword list have that a map does not?
Answer
A fixed order and repeated keys
A list keeps its order and may hold
{tag, a}and{tag, b}; a map keeps one value per key. -
What does
proplists:get_value(port, Opts)answer when the key is missing?Answer
undefinedWithout a default,
get_value/2answers the atomundefinedinstead of failing. -
What does
proplists:get_value(timeout, Opts, 5000)answer when the key is missing?Answer
5000The third argument is the default that comes back when no entry matches.
-
What does this module print?
-module(q1). -export([main/0]). main() -> Opts = [{port, 5433}, {timeout, 100}], io:format("~p~n", [proplists:get_value(port, Opts, 5432)]), io:format("~p~n", [proplists:get_value(host, Opts, "localhost")]).Answer
5433/"localhost"The port is present and wins over the default; the host is absent, so its default comes back.
-
What does this module print?
-module(q2). -export([main/0]). main() -> Opts = [{tag, normal}, {tag, alt}], io:format("~p~n", [proplists:get_value(tag, Opts)]), io:format("~p~n", [proplists:get_all_values(tag, Opts)]).Answer
normal/[normal,alt]get_value/2takes the first match, whileget_all_values/2collects both values in order. -
What does this module print?
-module(q3). -export([main/0]). main() -> Opts = [{port, 5432}, verbose], io:format("~p~n", [proplists:get_bool(verbose, Opts)]), io:format("~p~n", [proplists:get_value(verbose, Opts)]).Answer
true/trueA bare atom is shorthand for
{verbose, true}, so both functions see the option. -
What does this module print?
-module(q4). -export([main/0]). main() -> Opts = [{retries, 3}], io:format("~p~n", [proplists:is_defined(retries, Opts)]), io:format("~p~n", [proplists:is_defined(timeout, Opts)]).Answer
true/falseis_defined/2answers whether the key has an entry, without reading its value. -
What happens when
proplists:get_value/2receives a map instead of a list?Answer
It raises
function_clauseproplistswalks a list; a map is not a list, so no clause matches. -
When is a map the better choice than a keyword list?
Answer
When lookup speed matters
Maps are built for lookup; lists are built for order and repetition.
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.
Автор статьи: Arthur Isaev