The Elixir documentation for start_link(module, fun, args, options \\ [])
doesn’t have any example on how to use it:
@spec start_link(module(), atom(), [any()], GenServer.options()) :: on_start()
Starts an agent linked to the current process.
Same as start_link/2 but a module, function, and arguments are expected instead of an
anonymous function; fun in module will be called with the given arguments args to initialize the state.
My current code to initialize the Agent with a module name:
defmodule UdpServer.ServersAgent do
use Agent
alias UdpServer.ServersAgent
defstruct [
servers: %{},
tracked_packets: %{},
]
def start_link(args) do
IO.inspect(args, label: "SERVERS ARGS")
# Agent.start_link(fn -> %ServersAgent{} end, name: __MODULE__)
Agent.start_link(__MODULE__, :initial_state, args)
end
# Function isn't called as the docs seems to say.
def initial_state(args) do
IO.puts("initial_state/1")
%ServersAgent{}
end
# Function its called but the Agent isn't started
def initial_state() do
IO.puts("initial_state/0")
# %ServersAgent{}
fn -> %ServersAgent{} end
end
### omitted some code ... ###
end
What am I missing or doing wrong?