Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused variable warnings when variables created in map pattern match #33

Open
carusso opened this issue Aug 20, 2018 · 1 comment
Open

Comments

@carusso
Copy link

carusso commented Aug 20, 2018

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
@sasa1977
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants