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
97
97
The definition above wouldn't work for this particular example,
98
98
as it would attempt to start the counter with an initial value
99
99
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.
101
112
102
113
`use Agent` also accepts a list of options which configures the
103
114
child specification and therefore how it runs under a supervisor.
You can’t perform that action at this time.
0 commit comments