mikunn

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

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

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement