LukasKnuth

LukasKnuth

How do I unquote a sigil parameter to a macro?

Consider the following simple example:

defmodule Test.Foo do
  defmacro foo(regex) do
    IO.inspect(regex)
  end
end

defmodule Test do
  require Test.Foo

  Test.Foo.foo(~r/test/)
end

{:sigil_r, [delimiter: "/", line: 10], [{:<<>>, [line: 10], ["test"]}, []]}
iex>

Here, the sigil passed to Test.Foo.foo becomes a quoted expression. When using the macro for code generation, one would simple call unquote/1 on arg and get the sigil representation again.

However, if I do this in the above example (IO.inspect(unquote(regex))), I get the following error:

warning: variable “regex” does not exist and is being expanded to “regex()”, please use parentheses to remove the ambiguity or change the variable name
macro_why.exs:3: Test.Foo
** (CompileError) macro_why.exs:3: undefined function regex/0 (there is no such import)

Note that I don’t have a quote block in my macro. The actual code I’m working on parses the AST directly into an internal representation, so there is no need to quote anything. This is whats kinda throwing me for a loop here.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @LukasKnuth that isn’t how that works. You can think of quote and unquote as similar to "" and #{}. You can’t use #{} outside of a string "" it just doesn’t make any sense. And when you use unquote inside of quote it is not turning it “back into” a sigil, it’s still AST. AST is all there is at the macro level. It’s just interpolating that ast inside of other AST.

Can you show an example of the code you’re trying to get to work in general? Tangentially there is another thread about the use of ~p that is pretty similar Using `~p` dynamically inside a macro - #5 by 0xG it may have some useful pointers for you.

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement