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
Thanks a lot! That works for the original case and I understand what that is doing.
My example was lacking a bit, since I have other default implementations, so I added hello2/1 to the original post. params doesn’t exist in that case, so I tried to find a way to find out in the macro if params exists before using it, but I had no luck.
A solution that works for both cases is to add _ = params to hello/1, but there must a better way to do this without cluttering the original function because of the macro.
You can maybe use binding/1, but you’ll not know which in scope variable is used by the AST being the input to your macro without recursing through said AST. So blindly using it might silence ligitimate warnings.
I thought I could run some filtering on that inside quote, but I guess just calling binding() binds everything, so like you said, it might be best avoided.
I came up with this
defmacro build_impl(default) do
if Mix.env() == :dev do
if Macro.Env.has_var?(__CALLER__, {:params, nil}) do
quote do
_ = var!(params)
Myapp.MacroModule.fake_impl()
end
else
quote do: Myapp.MacroModule.fake_impl()
end
else
quote do: unquote(default)
end
end
It seems to work and will suffice for now since other callers to the macro have all the variables used inside the function. If I needed to check for multiple variables that would get clumsy.
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