a-maze-d

a-maze-d

Hi,

I’m trying to create some syntactic sugar through a macro. The basic function should look like the following:

f :name do
  data.something + 10
end

The macro will transform the body to an anonymous function. Yes the somewhat undefined data is intentional, it will be used to capture the argument that will be passed to the function.

I didn’t manage to create a macro that resolves the data correctly. I found now a solution that works by using the anonymous function of the capture operator. The downside is that I need to capture the argument through &1.something which does not look nice :frowning: (note the real code looks more complicated since it uses a compile hook, but the following is working)

defmodule F do
  defmacro f(name, options, do: block) do
    func = quote do
      &(unquote(block))
    end
    quote do
      register(unquote(name), unquote(options), unquote(func))
    end
  end

  def register(name, options, func) do
    result = func.(%{something: 10})
    IO.puts("#{inspect name}, #{inspect options}, #{inspect func}, #{inspect result}")
  end
end

defmodule G do
  def run() do
    import F
    F.f :name, [] do
      &1.something-10
    end
  end
end

And call it with:

import G
G.run

If I convert the capturing operator to an anonymous function it should work just fine. The doc does state:

In other words, &(&1 * 2) is equivalent to fn x -> x * 2 end.

But, whenI change my macro to look like this:

defmacro f(name, options, do: block) do
    func = quote do
      fn(data) -> unquote(block) end
    end
    quote do
      register(unquote(name), unquote(options), unquote(func))
    end
  end

I get the error:

warning: variable "data" does not exist and is being expanded to "data()", please use parentheses to remove the ambiguity or change the variable name
  lib/f.ex:21: G.run/0


== Compilation error in file lib/f.ex ==
** (CompileError) lib/f.ex:21: undefined function data/0 (expected G to define such a function or for it to be imported, but none are available)

Why is this? Can someone please help me to understand this?

Showing Posts 1 to 2

christhekeele

christhekeele

What you’re struggling with is called macro hygiene.

This macro would indeed expand this call:

f(:name, [option: :option]) do
  data.something + 10
end

into this code:

register(:name, [option: :option], fn(data) -> data.something + 10 end)

However, the compiler recognizes that the variable data has two different providences, and treats them as different variables; it sees things more like this:

register(:name, [option: :option], fn(data1) -> data2.something + 10 end)
#                                     ▲         ▲
# your macro's `data` ────────────────┘         │
# your user code's `data` ──────────────────────┘

Macro hygiene allows macro authors to not worry about their generated code stepping on other variables in scope, but requires more work when injecting user-provided code with your own variables.

You should be able to follow the guide linked above to decorate your macro’s data with a call to var! to escape hygiene.

Alternatively, and perhaps more idiomatically, you could allow the user code to dictate the variable name by having your macro expect clauses, similar to a case statement. Untested code snippet, that I’ve gotten to work with macros before; IIRC you have to build the AST for the fn by hand instead of using quote/unquote as there is no analog to unquote_splicing that works for clauses:

defmacro f(name, options, do: clauses) do
    func_ast = {:fn, [], clauses}
    quote do
      register(unquote(name), unquote(options), unquote(func_ast))
    end
  end
end

Then in user-code:

f(:name, [option: :option]) do
  data -> data.something + 10
end

This has the added benefit of letting users pattern-match and provide multiple clauses to handle different cases.

At this point though, you may well want them to just pass in a function–depending on your use-case.

a-maze-d

a-maze-d OP

Thanks for your swift help. It is indeed what you are stating. Thus the use of Kernel.var! solves that problem.

The reason why I didn’t manage to resolve it myself, because I didn’t realize the difference between Macro.var and Kernel.var!.

Thanks again for your help.

Ps: Your suggestion to allow the user to select the binding, is definitely something I wanted to allow, in a later version.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
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
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews