Skip to content

Commit 8218281

Browse files
author
José Valim
committed
More docs to child specs
1 parent 53f16f5 commit 8218281

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/elixir/lib/agent.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,18 @@ defmodule Agent do
9797
The definition above wouldn't work for this particular example,
9898
as it would attempt to start the counter with an initial value
9999
of an empty list. However, this may be a viable option in your
100-
own agents.
100+
own agents. A common approach is to use a keyword list, as that
101+
would allow setting the initial value and giving a name to the
102+
counter process, for example:
103+
104+
def start_link(opts) do
105+
{initial_value, opts} = Keyword.pop(opts, :initial_value, 0)
106+
Agent.start_link(fn -> initial_value end, opts)
107+
end
108+
109+
and then you can use `Counter`, `{Counter, name: :my_counter}` or
110+
even `{Counter, initial_value: 0, name: :my_counter}` as a child
111+
specification.
101112
102113
`use Agent` also accepts a list of options which configures the
103114
child specification and therefore how it runs under a supervisor.

0 commit comments

Comments
 (0)