I have been going over my notes and noting the places my students have trouble with standard Elixir documentation. One of them is this example code that starts a child for a dynamic supervisor:
{:ok, agent1} = DynamicSupervisor.start_child(MyApp.DynamicSupervisor, {Agent, fn -> %{} end})
Agent.update(agent1, &Map.put(&1, :key, "value"))
Agent.get(agent1, & &1)
#=> %{key: "value"}
{:ok, agent2} = DynamicSupervisor.start_child(MyApp.DynamicSupervisor, {Agent, fn -> %{} end})
Agent.get(agent2, & &1)
#=> %{}
DynamicSupervisor.count_children(MyApp.DynamicSupervisor)
#=> %{active: 2, specs: 2, supervisors: 0, workers: 2}
This code gives students two problems:
1. It expresses a concept using an arguably more complex concept, and not a common one: agents.
2. The child spec is not like typical gen servers because the initial value is a function.
I propose that we start a stack rather than an agent. Failing that, I propose we label the concepts in the start spec with intermediate variables.
-bt
--