It’s my first time using GenServer for Elixir and I am managing to run it but I am getting warnings for my function arity/name even though they don’t override etc. It’s better to explain with code, here’s my module:
defmodule Voicer.UserCache do
use GenServer
alias Voicer.User
# Client API
def start_link, do: GenServer.start(__MODULE__, %{}, name: __MODULE__)
def add_user(user), do: GenServer.cast(__MODULE__, {:add_user, user})
def get_user(username), do: GenServer.call(__MODULE__, {:get_user, username})
def remove_user(username), do: GenServer.cast(__MODULE__, {:remove_user, username})
def list_users(), do: GenServer.call(__MODULE__, :list_users)
# Server
def init(users), do: {:ok, users}
def handle_cast({:add_user, %User{username: username, key: key}}, users) do
users = users |> Map.put(username, key)
{:noreply, users}
end
def handle_call({:get_user, username}, _from, users) do
return = if Map.get(users, username) == nil, do: nil, else: %User{username: username, key: Map.get(users, username)}
{:reply, return, users}
end
def handle_call(:list_users, _from, users) do
return =
Enum.map users, fn({username, key}) ->
%User{username: username, key: key}
end
{:reply, return, users}
end
def handle_cast({:remove_user, username}, users), do: {:noreply, Map.delete(users, username)}
end
I get an arity warning on the :remove_user
and :list_users
functions:
warning: clauses with the same name and arity (number of arguments) should be grouped together, “def
handle_cast/2” was previously defined (lib/voicer/cache/user_cache.ex:21)
lib/voicer/cache/user_cache.ex:31warning: clauses with the same name and arity (number of arguments) should be grouped together, “def handle_call/3” was previously defined (lib/voicer/cache/user_cache.ex:26)
lib/voicer/cache/user_cache.ex:33
Am I naming/setting up them correctly/practically?