Unused variable warnings when variables created in map pattern match
I have some function headers like so:
defcall set_open_device(%{"path" => path, "baud" => speed}), state: state do
"path" and "speed" are used in the function, but I see the following compiler warnings:
warning: variable "path" is unused
lib/rni/dispatch/fmt.ex:53
warning: variable "speed" is unused
lib/rni/dispatch/fmt.ex:53
Hi,
Unfortunately, this is the consequence of implicit generation of two functions. Thedefcall expression in your example will lead to the following functions being generated:
def set_open_device(server, %{"path" => path, "baud" => speed} = payload) do
GenServer.call(server, {:set_open_device, payload})
end
def handle_call({set_open_device, %{"path" => path, "baud" => speed}}, _from, state) do
# ...
end
So the warning happens because path and speed are not used in the interface function.
A simple fix could be to pattern match in the body:
defcall set_open_device(payload), state: state do
%{"path" => path, "baud" => speed} = payload
# ...
end
This is one of the reasons why I personally don't use ExActor anymore, nor recommend using it. My advice is to stick to plain GenServer. It might mean a bit more typing, but at least it's explicit, with no magic in between, and you're in complete control of the code.