File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments