mikunn
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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











Showing Posts 1 to 8- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
LostKobrakai
Add the following to your fake implementation part of your macro.
mikunn
Thanks for the response!
I tried both
and
The first one complains that
paramsdoesn’t exist inside the macro and second complains thatparamsdoesn’t exist on the first line ofhello/1.Also, I have something like this as well:
I would assume adding both
_ = paramsand_ = paramwould not work?LostKobrakai
It needs to go inside the quote, but I forgot about macro hygiene. This should work
_ = var!(params)when the variable is not explicitly passed.mikunn
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/1to the original post.paramsdoesn’t exist in that case, so I tried to find a way to find out in the macro ifparamsexists before using it, but I had no luck.A solution that works for both cases is to add
_ = paramstohello/1, but there must a better way to do this without cluttering the original function because of the macro.LostKobrakai
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.mikunn
I thought I could run some filtering on that inside
quote, but I guess just callingbinding()binds everything, so like you said, it might be best avoided.I came up with this
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.
al2o3cr
Another approach: you could add a never-taken branch with the unused code in it:
mikunn
That’s pretty neat and simple! Thanks! Will mark this as solution.