mikunn
How to get rid of unused variable warning when replacing implementation with a macro?
I’m trying to replace a little functionality with a fake one in dev, while every other environment must use the default one. For security reasons, I don’t want the call to the fake functionality even exist in production, so I’m using a macro. Here’s the barebones idea:
defmodule Myapp.MacroModule do
defmacro build_impl(default) do
if Mix.env() == :dev do
quote do: Myapp.MacroModule.fake_impl()
else
quote do: unquote(default)
end
end
def fake_impl() do
:fake_value
end
end
defmodule Myapp do
require Myapp.MacroModule
def hello(params) do
result = Myapp.MacroModule.build_impl(Map.get(params, :key))
IO.inspect(result)
# do_something_else_with_result(result)
end
def hello2(param) do
Myapp.MacroModule.build_impl(param)
IO.inspect(param)
end
end
In dev it uses fake_impl/0 and in other envs the default one passed to the macro and it works fine. The macro can be called with different implementations from different places.
The issue is that I of course get variable "params" is unused warning in dev since to my understanding the first line of hello/1 would turn out to be result = Myapp.MacroModule.fake_impl() and params is not used.
How could I go about suppressing the warning? Or should I approach the original issue some other way?
Thanks for any help!
EDIT: added hello2/1 function to demonstrate another default implementation
Marked As Solved
al2o3cr
Another approach: you could add a never-taken branch with the unused code in it:
defmacro build_impl(default) do
if Mix.env() == :dev do
quote do
if false do
unquote(default)
end
Myapp.MacroModule.fake_impl()
end
else
quote do: unquote(default)
end
end







