Exadra37
Start an Agent with the module name instead of anonymous function
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?
Marked As Solved
LostKobrakai
args is a list of parameters not a value to pass. So you want to wrap args in a list to call the arity-1 function. This mimics how mfa data is passed to apply/3 and many other similar functions.
Agent.start_link(__MODULE__, :initial_state, [args])
1
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New







