mikunn

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

Showing Posts 1 to 8

LostKobrakai

LostKobrakai

Add the following to your fake implementation part of your macro.

_ = params
mikunn

mikunn OP

Thanks for the response!

I tried both

_ = params
quote do: Myapp.MacroModule.fake_impl()

and

quote do
  _ = params
  Myapp.MacroModule.fake_impl()
end

The first one complains that params doesn’t exist inside the macro and second complains that params doesn’t exist on the first line of hello/1.

Also, I have something like this as well:

def hello2(param) do
  result = Myapp.MacroModule.build_impl(param)
  IO.inspect(result)
  IO.inspect(param)
end

I would assume adding both _ = params and _ = param would not work?

LostKobrakai

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

mikunn OP

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.

LostKobrakai

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

mikunn OP

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.

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
mikunn

mikunn OP

That’s pretty neat and simple! Thanks! Will mark this as solution.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews